0% found this document useful (0 votes)
28 views15 pages

C++ Classes for Beginners

xfsdfsdfsdfsd

Uploaded by

Mohammed Ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views15 pages

C++ Classes for Beginners

xfsdfsdfsdfsd

Uploaded by

Mohammed Ibrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

C++

C++ Classes and Objects


C++ CLASSES AND OBJECTS

 In this tutorial, we will learn about objects and classes and how to use them
in C++ with the help of examples.
 In previous tutorials, 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.
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 SYNTAX
 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
 // 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,

 [Link]();
 This will call the calculateArea() function inside the Room class for object
room2.

 Similarly, the data members can be accessed as:

 [Link] = 5.5;
EXAMPLE 1: OBJECT AND CLASS IN C++ PROGRAMMING

// Program to illustrate the working of


int main() {
// objects and class in C++ Programming
// create object of Room class
#include <iostream>
Room room1;
using namespace std;
// assign values to data members
// create a class
[Link] = 42.5;
class Room {
[Link] = 30.8;
[Link] = 19.2;
public:
double length;
// calculate and display the area and volume of
double breadth;
the room
double height;
cout << "Area of Room = " <<
[Link]() << endl;
double calculateArea() {
cout << "Volume of Room = " <<
return length * breadth;
[Link]() << endl;
}
return 0;
double calculateVolume() {
}
return length * breadth * height;
}
};
 We then called the functions calculateArea() and calculateVolume() to
perform the necessary calculations.

 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.

 To learn more about public and private keywords, please visit our C++ Class
Access Modifiers tutorial.
EXAMPLE 2: USING PUBLIC AND PRIVATE IN C++ CLASS
double calculateArea() {
#include <iostream> return length * breadth;
using namespace std; }

class Room { double calculateVolume() {


return length * breadth * height;
private: }
double length; };
double breadth;
double height; int main() {

public: // create object of Room class


Room room1;
// function to initialize private variables
void initData(double len, double brth, double // pass the values of private variables as
hgt) { arguments
length = len; [Link](42.5, 30.8, 19.2);
breadth = brth;
height = hgt; cout << "Area of Room = " <<
} [Link]() << endl;
cout << "Volume of Room = " <<
[Link]() << endl;
 The above example is nearly identical to the first example, except that the class
variables are now private.

 Since the variables are now private, we cannot access them directly from main().
Hence, using the following code would be invalid:

 // invalid code
 [Link] = 42.5;
 [Link] = 30.8;
 [Link] = 19.2;
 Instead, we use the public function initData() to initialize the private variables via
the function parameters double len, double brth, and double hgt.

You might also like