CST 2nd Year OOP E-Contents - Unit 2 - Classes & Objects
CST 2nd Year OOP E-Contents - Unit 2 - Classes & Objects
Engineering Institute
Bishnupur – Bankura – 722122
Department of Computer Science & Technology
2nd Year – 4th Semester
e-Contents : Object Oriented Programming
Chapter : Objects & Classes (Unit : 2)
Developed By :
Raj Kumar Datta (Lecturer & HOD)
Computer Science & Technology
KGEI, Bishnupur, Bankura-
Bankura- 722122
1
Contents 2 Objects & Classes
2.1 Specifying a Class
Designing a member Functions
Arrays within a Class
Creating Objects
Memory Allocation for Objects
Static Data & Member Function
Array of Objects
Objects as function arguments
Returning objects from member function
Friend Function
Public member can be accessed with the help of an object (i.e. i1 or i2 etc.)
Private data member of a class can be accessed through the member function of
that class.
Object Oriented Programming : Objects & Classes –
5
KGEI – Bishnupur - Bankura
2.1 Defining member function
Member functions can be defined in two places:
- Inside the class definition - Outside the class definition.
Inside the class definition:
class Item{
private:
int number; // member variable declarations;
float cost;
public:
void getData(int a, float b) { number = a; cost = b;}
void putData(){
cout<< number << endl << cost << endl;
}
}; // end with semicolon
Object Oriented Programming : Objects & Classes –
6
KGEI – Bishnupur - Bankura
2.1 Defining member function : (Inside the Class)
Syntax :
return_type class_name :: function_name(argument(s)){
// function body
}
Example :
void Item :: getData(int a, float b){
number = a;
cost = b;
}
Syntax :
datatype class_name :: variable;
Example :
int item:: count; // count = 0
Note:
Static member function can be called using
class name and scope resolution operator
as shown below:
CLASS_NAME :: FUNCTION_NAME();
- Like array of other user-defined data types, an array of type class can also be created.
- The array of type class contains the objects of the class as its individual elements.
- An array of objects is declared in the same way as an array of any built-in data type.
Syntax :
class_name array_name [size];
Example:
student std[MAX]; // MAX is integer constant
The first method is called pass-by-value. Since a copy of the object is passed to the function.
Any changes made to the object inside the function do not affect the object to call the function.
The second method is called pass-by-reference. When an address of the object is passed, the called
function works directly on the actual object used in the call, means that any changes made to the
object inside the function will reflect. It is better to pass the address of the object in spite of
passing the entire object.
C++ access specifiers are private protected and public. These keywords are called access
specifiers which define the accessibility or visibility level of class members.
By default the class members are private i.e. if the visibility modes are not mentioned explicitly,
then by default all the class members become private.
public - members are accessible from outside the class as well as from that class.
private - members cannot be accessed from outside the class and are accessible from inside
class itself where it is declared.
protected - members cannot be accessed from outside the class, however, they can be accessed
in inherited classes i.e. from derived classes.
Object Oriented Programming : Objects & Classes –
24
KGEI – Bishnupur - Bankura
2.2 Distinction between structure of C and Class in C++