UNIT 5
1 Basics of object and class in C++
Class: A class in C++ is the building block that leads to Object-Oriented programming. It is a user-defined data
type, which holds its own data members and member functions, which can be accessed and used by creating an
instance of that class. A C++ class is like a blueprint for an object.
For Example: Consider the Class of Cars. There may be many cars with different names and brand but all of
them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range etc. So
here, Car is the class and wheels, speed limits, mileage are their properties.
A Class is a user defined data-type which has data members and member functions.
Data members are the data variables and member functions are the functions used to manipulate these
variables and together these data members and member functions defines the properties and behavior of the
objects in a Class.
In the above example of class Car, the data member will be speed limit, mileage etc and member functions
can be apply brakes, increase speed etc.
An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated
(i.e. an object is created) memory is allocated.
A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside
the curly brackets and terminated by a semicolon at the end.
Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or
storage is allocated. To use the data and access functions defined in the class, you need to create objects.
Syntax:
ClassName ObjectName;
Private and public members
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. The public members of a class can be accessed
from anywhere in the program using the direct member access operator (.) with the object of that class.
Example:
// C++ program to demonstrate public
// access modifier
#include <iostream>
using namespace std;
// class definition
class Circle {
public:
double radius;
double compute_area()
{
return 3.14 * radius * radius;
}
};
// main function
int main()
{
Circle obj;
// accessing public data member outside class
obj.radius = 5.5;
cout << "Radius is: " << obj.radius << "\n";
cout << "Area is: " << obj.compute_area();
return 0;
}
Output:
Radius is: 5.5
Area is: 94.985
In the above program, the data member radius is public so we are allowed to access it outside the class.
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.
Example:
// C++ program to demonstrate private
, // access modifier
#include <iostream>
using namespace std;
class Circle {
// private data member
private:
double radius;
// public member function
public:
void compute_area(double r)
{
// member function can access private
// data member radius
radius = r;
double area = 3.14 * radius * radius;
cout << "Radius is: " << radius << endl;
cout << "Area is: " << area;
}
};
// main function
int main()
{
// creating object of the class
Circle obj;
// trying to access private data member
// directly outside the class
obj.compute_area(1.5);
return 0;
}
Output:
Radius is: 1.5
Area is: 7.065
2 Static data and function members
Static data members are class members that are declared using static keywords. A static member has certain
special characteristics. These are:
, Only one copy of that member is created for the entire class and is shared by all the objects of that class, no
matter how many objects are created.
It is initialized before any object of this class is being created, even before main starts.
It is visible only within the class, but its lifetime is the entire program
Syntax
static data_type data_member_name;
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A's Constructor Called " << endl; }
};
class B
{
static A a;
public:
B() { cout << "B's Constructor Called " << endl; }
};
int main()
{
B b;
return 0;
}
Output:
B's Constructor Called
The above program calls only B’s constructor, it doesn’t call A’s constructor. The reason for this is simple, static
members are only declared in a class declaration, not defined. They must be explicitly defined outside the class
using the scope resolution operator.
If we try to access static member ‘a’ without an explicit definition of it, we will get a compilation error.
3 Constructors and their types
A constructor is a special type of member function of a class which initializes objects of a class. In C++,
Constructor is automatically called when object(instance of class) create. It is special member function of the
class because it does not have any return type.
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.
It must be placed in public section of class.
1 Basics of object and class in C++
Class: A class in C++ is the building block that leads to Object-Oriented programming. It is a user-defined data
type, which holds its own data members and member functions, which can be accessed and used by creating an
instance of that class. A C++ class is like a blueprint for an object.
For Example: Consider the Class of Cars. There may be many cars with different names and brand but all of
them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range etc. So
here, Car is the class and wheels, speed limits, mileage are their properties.
A Class is a user defined data-type which has data members and member functions.
Data members are the data variables and member functions are the functions used to manipulate these
variables and together these data members and member functions defines the properties and behavior of the
objects in a Class.
In the above example of class Car, the data member will be speed limit, mileage etc and member functions
can be apply brakes, increase speed etc.
An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated
(i.e. an object is created) memory is allocated.
A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside
the curly brackets and terminated by a semicolon at the end.
Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or
storage is allocated. To use the data and access functions defined in the class, you need to create objects.
Syntax:
ClassName ObjectName;
Private and public members
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. The public members of a class can be accessed
from anywhere in the program using the direct member access operator (.) with the object of that class.
Example:
// C++ program to demonstrate public
// access modifier
#include <iostream>
using namespace std;
// class definition
class Circle {
public:
double radius;
double compute_area()
{
return 3.14 * radius * radius;
}
};
// main function
int main()
{
Circle obj;
// accessing public data member outside class
obj.radius = 5.5;
cout << "Radius is: " << obj.radius << "\n";
cout << "Area is: " << obj.compute_area();
return 0;
}
Output:
Radius is: 5.5
Area is: 94.985
In the above program, the data member radius is public so we are allowed to access it outside the class.
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.
Example:
// C++ program to demonstrate private
, // access modifier
#include <iostream>
using namespace std;
class Circle {
// private data member
private:
double radius;
// public member function
public:
void compute_area(double r)
{
// member function can access private
// data member radius
radius = r;
double area = 3.14 * radius * radius;
cout << "Radius is: " << radius << endl;
cout << "Area is: " << area;
}
};
// main function
int main()
{
// creating object of the class
Circle obj;
// trying to access private data member
// directly outside the class
obj.compute_area(1.5);
return 0;
}
Output:
Radius is: 1.5
Area is: 7.065
2 Static data and function members
Static data members are class members that are declared using static keywords. A static member has certain
special characteristics. These are:
, Only one copy of that member is created for the entire class and is shared by all the objects of that class, no
matter how many objects are created.
It is initialized before any object of this class is being created, even before main starts.
It is visible only within the class, but its lifetime is the entire program
Syntax
static data_type data_member_name;
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A's Constructor Called " << endl; }
};
class B
{
static A a;
public:
B() { cout << "B's Constructor Called " << endl; }
};
int main()
{
B b;
return 0;
}
Output:
B's Constructor Called
The above program calls only B’s constructor, it doesn’t call A’s constructor. The reason for this is simple, static
members are only declared in a class declaration, not defined. They must be explicitly defined outside the class
using the scope resolution operator.
If we try to access static member ‘a’ without an explicit definition of it, we will get a compilation error.
3 Constructors and their types
A constructor is a special type of member function of a class which initializes objects of a class. In C++,
Constructor is automatically called when object(instance of class) create. It is special member function of the
class because it does not have any return type.
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.
It must be placed in public section of class.