Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

C++ Classes and Objects

The document provides an overview of C++ classes and objects, explaining the principles of object-oriented programming. It details how to define classes, create objects, and access data members and member functions, along with the concepts of public and private access modifiers. Additionally, it covers constructors, including default, parameterized, and copy constructors, illustrating their use with examples.

Uploaded by

agentmax360
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C++ Classes and Objects

The document provides an overview of C++ classes and objects, explaining the principles of object-oriented programming. It details how to define classes, create objects, and access data members and member functions, along with the concepts of public and private access modifiers. Additionally, it covers constructors, including default, parameterized, and copy constructors, illustrating their use with examples.

Uploaded by

agentmax360
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

C++ Classes and Objects

• We learned about functions and variables.


• Sometimes it's desirable to put related functions and data in one
place so that it's logical and easier to work with.
• Suppose, we need to store the length, breadth, and height of a
rectangular room and calculate its area and volume.
• To handle this task, we can create three variables, say, length,
breadth, and height along with the functions calculateArea() and
calculateVolume().
• However, in C++, rather than creating separate variables and
functions, we can also wrap these related data and functions in a
single place (by creating objects).
• This programming paradigm is known as object-oriented
programming.
• But before we can create objects and use them in C++, we first
need to learn about classes.
C++ Class
• A class is a blueprint for the object.
• We can think of a class as a sketch (prototype) of a house.
• It contains all the details about the floors, doors, windows, etc.
• Based on these descriptions we build the house.
• House is the object.
Create a Class
A class is defined in C++ using keyword class followed by the
name of the class.
The body of the class is defined inside the curly brackets and
terminated by a semicolon at the end.
class className {
// some data
// some functions
};
//For example
class Room {
public:
double length;
double breadth;
double height;
double calculateArea(){
return length * breadth;
}
double calculateVolume(){
return length * breadth * height;
}
};
• Here, we defined a class named Room.
• The variables length, breadth, and height declared
inside the class are known as data members. And,
the functions calculateArea() and calculateVolume()
are known as member functions of a class.
C++ 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, we
need to create objects.
• Syntax to Define Object in C++
className objectVariableName;
We can create objects of Room class (defined in the above
example) as follows:
// sample function
void sampleFunction() {
// create objects
Room room1, room2;
}

int main(){
// create objects
Room room3, room4;
}
• Here, two objects room1 and room2 of the Room class are created
in sampleFunction(). Similarly, the objects room3 and room4 are
created in main().
• As we can see, we can create objects of a class in any function of
the program. We can also create objects of a class within the class
itself, or in other classes.
• Also, we can create as many objects as we want from a single
class.
C++ Access Data Members and Member Functions
• We can access the data members and member functions of
a class by using a . (dot) operator.
• For example, room2.calculateArea(); This will call the
calculateArea() function inside the Room class for object
room2.
• Similarly, the data members can be accessed as:
• room1.length = 5.5; In this case, it initializes the length
variable of room1 to 5.5.
// objects and class in C++ Programming int main() {
#include <iostream>
using namespace std; // create object of Room class
// create a class Room room1;
class Room { // assign values to data members
public: room1.length = 42.5;
double length; room1.breadth = 30.8;
double breadth; room1.height = 19.2;
double height;
double calculateArea() { // calculate and display the area and volume of the room
return length * breadth; cout << "Area of Room = " <<
} room1.calculateArea() << endl;
cout << "Volume of Room = " <<
double calculateVolume() { room1.calculateVolume() << endl;
return length * breadth * height;
} return 0;
}; }
• Note the use of the keyword public in the program. This
means the members are public and can be accessed
anywhere from the program.
• As per our needs, we can also create private members
using the private keyword.
• The private members of a class can only be accessed from
within the class.
• For example,
class Test {

private:
int a;
void function1() { }

public:
int b;
void function2() { }
}
Here, a and function1() are private. Thus they cannot be accessed from outside the
class.
On the other hand, b and function2() are accessible from everywhere in the program.
// public and private in C++ Class }
#include <iostream> double calculateVolume() {
using namespace std; return length * breadth * height;
class Room { }
private: };
double length; int main() {
double breadth; // create object of Room class
double height; Room room1;
public: // pass the values of private variables as
// function to initialize private variables arguments
void initData(double len, double brth, double hgt) room1.initData(42.5, 30.8, 19.2);
{ cout << "Area of Room = " <<
length = len; room1.calculateArea() << endl;
breadth = brth; cout << "Volume of Room = " <<
height = hgt; room1.calculateVolume() << endl;
} return 0;
double calculateArea() { }
return length * breadth;
C++ Constructors
A constructor is a special type of member function that is called automatically when
an object is created.
In C++, a constructor has the same name as that of the class and it does not have a
return type. For example,
class Wall {
public:
// create a constructor
Wall() {
// code
}
};
Here, the function Wall() is a constructor of the class Wall. Notice that the
constructor has the same name as the class, does not have a return type, and is
public
C++ Default Constructor

 A constructor with no parameters is known as a


default constructor. In the example above, Wall() is
a default constructor.
• Example 1: C++ Default Constructor
#include <iostream> }
using namespace std; };
// declare a class
class Wall { int main()
private: {
double length; Wall wall1;
public: return 0;
// default constructor to initialize variable }
Wall() {
length = 5.5;
cout << "Creating a wall." << endl;
cout << "Length = " << length <<
endl;
Output
Creating a Wall
Length = 5.5
• Here, when the wall1 object is created, the Wall() constructor is
called. This sets the length variable of the object to 5.5.
• Note: If we have not defined a constructor in our class, then the
C++ compiler will automatically create a default constructor with
an empty code and no parameters.
C++ Parameterized Constructor
• In C++, a constructor with parameters is known as a
parameterized constructor. This is the preferred method to
initialize member data.
• Example 2: C++ Parameterized Constructor
// C++ program to calculate the area of a wall
#include <iostream> }
using namespace std; };
// declare a class
class Wall { int main()
private: {
double length; // create object and initialize data members
double height; Wall wall1(10.5, 8.6);
public: Wall wall2(8.5, 6.3);
// parameterized constructor to initialize variables cout << "Area of Wall 1: " <<
Wall(double len, double hgt) { wall1.calculateArea() << endl;
length = len; cout << "Area of Wall 2: " <<
height = hgt; wall2.calculateArea();
} return 0;
double calculateArea() { }
return length * height;
Output
Area of Wall 1: 90.3
Area of Wall 2: 53.55

• Here, we have created a parameterized constructor Wall()


that has 2 parameters: double len and double hgt. The
values contained in these parameters are used to initialize
the member variables length and height.
• When we create an object of the Wall class, we pass the values
for the member variables as arguments. The code for this is:

• Wall wall1(10.5, 8.6);


• Wall wall2(8.5, 6.3);
• With the member variables thus initialized, we can now calculate
the area of the wall with the calculateArea() function.
C++ Copy Constructor
• The copy constructor in C++ is used to copy data of one
object to another.

• Example 3: C++ Copy Constructor


#include <iostream> int main() {
using namespace std; // create an object of Wall class
// declare a class Wall wall1(10.5, 8.6);
class Wall { // copy contents of wall1 to wall2
private: Wall wall2 = wall1;
double length; // print areas of wall1 and wall2
double height; cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
public: cout << "Area of Wall 2: " << wall2.calculateArea();
// initialize variables with parameterized constructor return 0;
Wall(double len, double hgt) { }
length = len;
height = hgt;
}
// copy constructor with a Wall object as parameter
// copies data of the obj parameter
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}
double calculateArea() {
return length * height;
}
};

You might also like