Professional Documents
Culture Documents
Classes and Object in C++: Dr. Munesh Singh
Classes and Object in C++: Dr. Munesh Singh
Object in
C++
Dr. Munesh
Classes and Object in C++
Singh
Classes and
Object in
C++
Dr. Munesh
Singh
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
Classes and
Object in
C++
Dr. Munesh
Singh Objective
Static local variables
Static member variables
Static member functions
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
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++
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
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