Week4a_Classes and Objects
Week4a_Classes and Objects
Constructors and
Destructors
• The main purpose of C++ programming is to add object orientation to the C programming
language and classes are the central feature of C++ that supports object-oriented
programming and are often called user-defined types.
• When you define a class, you define a blueprint for a data type.
• A class is used to specify the form of an object and it combines data representation and
methods for manipulating that data into one neat package.
• The data and functions within a class are called members of the class.
Classes and Objects: Syntax
• A class definition starts with the
keyword class followed by the class
name; and the class body, enclosed by
a pair of curly braces.
• A class definition must be followed
either by a semicolon or a list of
declarations.
class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a
box
double height; // Height of a box
};
Objects
• An instance of a class is called an
object. When a class is defined, no
memory is allocated, we only define
the specifications for its object.
• Memory is allocated when we
create an object of a class.
• Data members and member
functions of a class can be used and
accessed by creating objects.
• We can create multiple objects of a
class.
•Creating Objects in C++
ClassName ObjectName;
ACCESS SPECIFIERS
•PUBLIC
•PRIVATE
•PROTECTED
•
PUBLIC ACCESS SPECIFIERS -:
class ABC
{
public : // Public access modifiers
int x; // data member
void display(); //member function
};
PRIVATE ACCESS SPECIFIERS:-
•Private keyword means that no one can access the class members
declared private, outside the class.
•If someone tries to access the private number of a class, they will get
a compile time error.
class ABC
{
private : // Private access modifiers
int x; // data member
void display(); //member function
};
PROTECTED ACCESS SPECIFIERS -:
class ABC
{
protected : // Protected access modifiers
int x; // data member
void display(); //member function
};
Constructors in C++
• What is constructor?
A constructor is a member function of a class which initializes
objects of a class. In C++, Constructor is automatically called
when objects(instance of class) are created. It is special
member function of the class.
• How constructors are different from a normal member
function?
• A constructor is different from normal functions in following
ways:
• Constructor has same name as the class itself
• Constructors don’t have return type
• A constructor is automatically called when an object is created.
• If we do not specify a constructor, C++ compiler generates a
default constructor for us (expects no parameters and has an
empty body).
• Default Constructors: Default constructor is the
constructor which doesn’t take any argument. It has no
parameters.
#include <iostream>
using namespace std;
Construc public:
int a, b;
tors construct()
{
// Default Constructor
a = 10;
b = 20;
}
};
int main()
{
construct c; // Default constructor called
automatically
cout << "a: " << c.a << endl
<< "b: " << c.b;
return 1;
• It is possible to pass arguments to constructors.
• Typically, these arguments help initialize an object when it is
created. To create parameterized constructor, simply add
parameters to it the way you would to any other function. When
you define the constructor’s body, use the parameters to initialize
the object.
Constructors public:
int getX()
{
return x;
}
int getY()
{
return y;
}
int main()
{
Point p1(10, 15); // Constructor called
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
return 0;
}
Output:
p1.x = 10, p1.y = 15
Here, obj is a reference to an object that is being used to initialize another object.
Example of
Copy
Constructor
Output: 20
• What is destructor?
Destructor is a member function which destructs or deletes an
object.
• When is destructor called?
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 local variables ends
(4) a delete operator is called
• How destructors are different from a normal member
function?
Destructors have same name as the class preceded by a tilde
(~)
Destructors don’t take any argument and don’t return anything
class String
{
private:
char *s;
int size;
public:
String(char *); // constructor
~String(); // destructor
};
String::String(char *c)
{
size = strlen(c);
s = new char[size+1];
strcpy(s,c);
}
String::~String()
{
delete []s;
}
• Can there be more than one destructor in a class?
No, there can only one destructor in a class with class name
preceded by ~, no parameters and no return type.
• When do we need to write a user-defined destructor?
If we do not write our own destructor in class, compiler creates
a default destructor for us. The default destructor works fine
unless we have dynamically allocated memory or pointer in
class. When a class contains a pointer to memory allocated in
class, we should write a destructor to release memory before
the class instance is destroyed. This must be done to avoid
memory leak.
Nesting of Member function
• Only the public members of a class can be accessed by the object of
that class, using dot operator.
#include<iostream>
using namespace std
class nest
{
int a;
int square_num( )
{
return a* a;
}
A member function can public:
call another member void input_num( )
function of the same {
cout<<”\nEnter a number ”;
class for that you do not cin>>a;
need an object }
int cube_num( )
{
return a* a*a;
}
void disp_num()
{
int sq=square_num(); //nesting of member function
int cu=cube_num(); //nesting of member function
cout<<”\nThe square of “<<a<<” is ” <<sq;
cout<<”\nThe cube of “<<a<<” is ” <<cu;
} };
int main() Output:
{ Enter a number 5
nest n1; The square of 5 is 25
n1.input_num(); The cube of 5 is 125
n1.disp_num();
Write a C++ program to implement
a class called Circle that has private
member variables for radius.
Include member functions to
calculate the circle's area and
circumference.
Exercise
Questions Write a C++ program to create a
class called Car that has private
member variables for company,
model, and year. Implement
member functions to get and set
these variables.
Solution
Thank You