Professional Documents
Culture Documents
2 .Classes and Objects
2 .Classes and Objects
struct complex
{
float p;
float q;
};
struct complex c1, c2, c3 ;
✔ The complex numbers c1, c2 & c3 can easily be assigned values using the dot operator, but
we cannot add two complex numbers or subtract one from the other.
✔ For example, c3 = c1 + c2 ; is illegal in C.
✔ Another important limitation of C structures is that they do not
permit data hiding.
✔ Structure members can be easily accessed by the structure variables by any function
anywhere in their scope.
1. Class Declaration.
2. Class Function Definition.
✔ The Class Function Definitions describes how the class functions are
implemented.
Class Syntax
class class_name
{
Access Specifier:
Data Members;
Member Functions();
{
Statements;
}
};
In C++, there are three access specifiers:
● public - members are accessible from outside the class
● private - members cannot be accessed (or viewed) from outside the class
● protected - members cannot be accessed from outside the class, however,
they can be accessed in inherited classes.
Class Declaration
class class_name
{
private :
Variable
declarations ;
function
declarations ;
public :
variable
declarations ;
function
declarations ;
A simple Class Example
class item
{
int number; // Variables
float cost ; Declaration
// Private by Default
public :
void getdata (int a, float b) ; // Functions
void putdata (void) ; declaration
}; // using prototype
// Ends with Semicolon
Defining Member Function
✔ Member functions can be defined in 2 places :
✔ However, there is a subtle difference in the way the function header is defined.
Outside the Class Definition
✔ Member functions that are declared inside a class have to be defined
separately outside the class.
✔ Their definitions are very much like the normal functions.
int main ()
{
item obj1; // create object
obj1.getdata(100,2.60); // call member function
obj1.putdata ( ); // call member function
item obj2; // create object x
obj2.getdata (200, // call member
42.06) ; function
obj2.putdata ( ); // call member
return 0 ; function
}
********************************************************
✔ The use of statements such as
number = a ;
#include<iostream> #include<iostream>
using namespace std; using namespace std;
void counter(); void counter();
main() main()
{ {
for(int i=0;i<5;i++) for(int i=0;i<5;i++)
{ {
counter(); counter();
} }
} }
Static Data1
Static Data 2
Static Data K
#include < iostream >
using namespace std ;
class item
{
static int count ; //Static Variable Dec.
int number ;
public :
void getdata (int a)
{
number = a ;
count ++ ;
}
count
(Common to all 3 objects)
Initial condition of count variable
Object 1 Object 2 Object 3
number number number
100
count
(Common to all 3 objects)
Object 1 Object 2 Object 3
number number number
100 200
count
(Common to all 3 objects)
Object 1 Object 2 Object 3
number number number
count
(Common to all 3 objects)
Static Member Function
class-name : : function-name;
# include <iostream>
using namespace std ;
class test
{
int code ;
static int count ; // Static member variable
public :
void setcode (void)
{
code = ++ count ;
}
void showcode (void)
{
cout << “Object Number => ” << code << “\n”;
}
static void showcount (void) // Static member function
{
cout << “Count => ” << count << “\n”;
}
};
int test : : count ;
int main ()
{
test t1, t2 ;
test : : showcount () ; // Accessing Static Function
t1.setcode () ;
t2.setcode () ;
t1.showcount () ;
t2.showcount() ;
t3.showcount () ;
return 0;
}
Arrays of Objects
class employee
{
char name [30] ;
float age ;
public :
void getdata (void) ;
void putdata (void) ;
};
cout << “ T2 = ” ;
T2.puttime () ; // display T2
cout << “ T3 = ” ;
T3.puttime () ; // display T3
return 0 ;
}
● Default Constructor
● Parameterized Constructor
● Copy Constructor
Default Constructor
class Integer
{
int m,n;
public :
Integer() Default Constructor
{
m=0;
n= 0;
}
---------
---------
};
Parameterized Constructor
class Integer
{
int m,n;
public :
Integer(int a, int b) Parameterized Constructor
{
m=a;
n= b;
}
---------
---------
};
❖ When a constructor has been parameterized, the object declaration
statement such as
data d1 = data(10,20)
data d1(10,20)
● The following declaration illustrates the first method :-
integer int1 = integer (0, 100) ;// Explicit Call
● This statement creates an integer object int1 & passes the values 0 &
100 to it.
Object1 m = 0
n = 100
Object2 m = 25
n = 75
Copy Constructor
A copy constructor is the constructor that C++ uses to make copies of objects. Copy constructor is
called when a new object is created from an existing object as a copy of existing object.
class integer
{
int i;
public :
integer() Default Constructor
{
i =0;
}
void display()
{
cout <<i<<endl;
}
};
int main()
{
integer i1, i2(i1);
cout<<”i1 = “;
i1.display();
cout<<”i2 = “;
i2.display();
}
i1= 0
i2= 0
● A copy constructor is used to declare & intialize an object from another object.
● For example, the statement integer I2 (I1) ;
● Would define the object I2 & at the same time initialize it to the values of I1.
● Another form of this statement is integer I2 = I1 ;
integer i1;
Would automatically invoke the 1st constructor & set both m & n of
Destructors
➢ A destructor is used to destroy the objects that have been
created by a constructor.
➢ Destructor is a member function whose name is the same as
the class name but is preceded by a tilde.
~ integer ()
{
}
➢ A destructor never takes any argument nor does it return any
value.
➢ It will be invoked implicitly by the compiler upon exit from the
program (or block or function as the case may be) to clean
up storage that is no longer accessible.
➢ It is a good practice to declare destructors in a program
since it releases memory space for future use.
Example Code
~Integer()
{
cout << “Object is about to destroy”
<<endl;
}
Thank You