Sie sind auf Seite 1von 18

Classes and

Object in
C++

Dr. Munesh
Classes and Object in C++
Singh

Dr. Munesh Singh

Indian Institute of Information Technology


Design and Manufacturing,
Kancheepuram
Chennai-600127

December 26, 2018


Class and Strucutre

Classes and
Object in
C++

Dr. Munesh
Singh

The only difference between structure and class is that


the members of structure are by default public and
the members of class are by default private.
Example

Classes and
Object in struct complex
C++

Dr. Munesh {
Singh
private:
int a,b;
public:
void setdata(int x, inty) {
a=x; b=y; }
void show(){
cout<<”a”<<a<<”b”<<b;
};
void main(){
complex c1;
c1.setdata(3,4);
Function Call by Passing Object and returning
object
Classes and
Object in
C++
How to add two complex numbers?
Dr. Munesh
Singh
Technical jargons

Classes and
Object in
C++

Dr. Munesh
Singh

Class is a description of an object


Object is an instance of a class
Instance member variable
Attributes, data members, fields, properties
Instance member functions
Methods, procedures, actions, operations, services
Static Members in C++

Classes and
Object in
C++

Dr. Munesh
Singh Objective
Static local variables
Static member variables
Static member functions

Static Local Varaible


Concept as it is taken from C
They are by default initialized to zero
Their lifetime is throughout the program
Static Local Variable

Classes and
Object in
C++

Dr. Munesh
Singh

Example
void fun() {
static int x;
int y;
}
Static Member Varaible

Classes and
Object in
C++

Dr. Munesh
Singh Declared inside the class body
Also known as class member variable
They must be defined outside the class
Static member variable does not belong to any object, but
to the whole class.
There will be only one copy of static member variable for
the whole class
Any object can use the same copy of class variable
They can also be used with class name
Example

Classes and
Object in
C++
Class Account {
Dr. Munesh
Singh private:
int balance;instance member variable
static float roi;Static member variable/class variable
public:
void setbalance(int b)
balance=b; };
float Account::roi=3.5;
void main(){
Account a1;
}
Static Member Function

Classes and
Object in
C++

Dr. Munesh
Singh

They are qualified with the keyword static


They are also called member functions
They can be invoked with or without object
They can only access static members of the class
Example

Classes and
Object in
C++
Class Account {
Dr. Munesh private:
Singh
int balance;instance member variable
static float roi;Static member variable/class variable
public:
void setbalance(int b)
balance=b;
static void setRoi(float r) roi=r;};
float Account::roi=3.5;
void main(){
Account a1;
Account::setRoi(4.5);
}
Constructors in C++

Classes and
Object in
C++

Dr. Munesh Constructor


Singh
Constructor is a member function of a class
The name of the constructor is same as the name of the
class
It has no return type, so can’t use return keyword
It must be an instance member function, that is it can
never be static

How to call a Constructor


Constructor is implicitly invoked when an object is created.
Constructor is used to solve problem of initialization.
Example of Constructor

Classes and
Object in
C++
class complex {
Dr. Munesh
Singh private:
int a,b;
public:
Complex()
count¡¡”hello constructor”
};
int main()
{
complex c1,c2,c3;
}
What is problem of initialization?

Classes and
Object in
C++

Dr. Munesh
Singh

Why it is called Constructor?


What is problem if initialization?
How Constructor resolves this issue?
Types of Constructor

Classes and
Object in
C++

Dr. Munesh
Singh
Objective
Default Constructor
Parameterized Constructor
Constructor Overloading
In class no constructor in not defined, then it create two
constructor default and copy constructor.
If we have defined any one constructor, then only copy
constructor
If both the constructor, we have defined, then compiler
will not create any constructor
Example

Classes and
Object in Copy Constructor
C++

Dr. Munesh
Class Complex {
Singh
private:
int a,b;
public:
Complex(int x, int y)Parametrized constructor
{ a=x;b=y;}
Complex(int x)
{a=x;}
Complex default constructor
{a=0;b=0;}
Complex(Complex &c) {a=c.a;b=c.b;} };
int main(){
Example

Classes and
Object in
C++ Copy Construction
Dr. Munesh
Singh
Class Complex {
private:
int a,b;
public:
Complex(int x, int y)Parametrized constructor
{ a=x;b=y;}
Complex(int x)
{a=x;}
Complex default constructor
{a=0;b=0;} };
int main(){ Complex c1(2,3), c2(4),c3; }
Destructor

Classes and
Object in Destructor is an instance member function of a class
C++

Dr. Munesh The name of the destructor is same as the name of a class
Singh
but preceded by tilde symbol
Destructor can never be static
Destructor has no return type
Destructor takes no argument (No overloading is possible)
It is invoked implicitly when object is going to destroy.
Why destructor?
It should be defined to release resorces allocated to an
object

Das könnte Ihnen auch gefallen