Module2 1
Module2 1
2
Amity School of Engineering & Technology
Class:
A class is a definition of objects of the same kind.
A class is a blueprint, template, or prototype that defines and describes the static
attributes and dynamic behaviors common to all objects of the same kind.
Object:
It is an instantiation of a class.
Object is an instance of a class.
All the members of the class can be accessed through object.
Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.
Object is an entity that has state and behavior.
Here, state means data and behavior means functionality.
Amity School of Engineering & Technology
……
public:
void set(int w, int l);
int area(); r1.width=12;
}; r1.length=12;
8
Object & classes Amity School of Engineering & Technology
class Test
{
private: This class has:
int data1;
float data2;
Two data members:
public:
data1
void function1() data2
{ Two member functions:
data1 = 2; function1()
}
float function2()
function2()
{
data2 = 3.5;
return data2;
}
};
Amity School of Engineering & Technology
Public:
• All the class members declared under public will be
available to everyone. The data members and member
functions declared public can be accessed by other
classes too.
Amity School of Engineering & Technology
Private:
• The class members declared as private can be accessed
only by the functions inside the class. They are not
allowed to be accessed directly by any object or function
outside the class. Only the member functions or
the friend functions are allowed to access the private
data members of a class.
Amity School of Engineering & Technology
Protected:
• Protected access modifier is similar to that of private
access modifiers, the difference is that the class member
declared as Protected are in accessible outside the class
but they can be accessed by any subclass(derived class)
of that class.
Amity School of Engineering & Technology
Private Yes No No
};
Constructors
• A constructor is a Special member function which is used to initialize
data members
• It must be a public member function
• It must be named the same as the class
• It must have no return type
• automatically called when an object of the class is created
Amity School of Engineering & Technology
Examples
Types of Constructors
• Default Constructor
• Parameterized Constructor
• Copy Constructor
Amity School of Engineering & Technology
Types of Constructors
• Three Types of Constructor
Amity School of Engineering & Technology
Example:
class X
{
public:
X( ); // Default constructor with no arguments
X (int = 0); // Default constructor with one default argument
};
Default Constructors Amity School of Engineering & Technology
Parameterized Constructor helps you to assign initial value to an object at the time of its
creation
Copy Constructor
• Constructor that is used to initialize and declare an object from another
object is known as a copy constructor in C++.
33
Copy Constructors Amity School of Engineering & Technology
• Deletes an object.
• A destructor function is called automatically when the object goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing temporary variables ends
(4) a delete operator is called
• A destructor has:
(i) the same name as the class but is preceded by a tilde (~)
(ii) no arguments and return no values
(iii) Only 1 destructor is allowed per class (i.e., it cannot be overloaded)
Destructors
using namespace std;
Example
class Demo {
private: int num1, num2;
public: Demo(int n1, int n2)
{ cout<<"Inside Constructor"<<endl;
num1 = n1; num2 = n2; }
void display()
{ cout<<"num1 = "<< num1 <<endl;
cout<<"num2 = "<< num2 <<endl; }
~Demo() { cout<<"Inside Destructor"; }
};
int main() { Demo obj1(10, 20); obj1.display(); return 0; }
Local Class and GlobalAmity
ClassSchool of Engineering & Technology
• All the methods of Local classes must be defined inside the class only
• A Local class cannot contain static data members. It may contain static
functions though.
• Local classes can access global types, variables and functions. Also, local
classes can access other local classes of same function..
Amity School of Engineering & Technology
Thank You