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

Module 1 Chapter 2

This document provides an overview of classes and objects in C++, explaining how to encapsulate related data and functions using object-oriented programming. It includes examples of defining classes, creating objects, accessing data members and member functions, and demonstrates the concept of friend classes and functions. The document also illustrates the syntax and structure of C++ code for various scenarios involving classes and objects.

Uploaded by

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

Module 1 Chapter 2

This document provides an overview of classes and objects in C++, explaining how to encapsulate related data and functions using object-oriented programming. It includes examples of defining classes, creating objects, accessing data members and member functions, and demonstrates the concept of friend classes and functions. The document also illustrates the syntax and structure of C++ code for various scenarios involving classes and objects.

Uploaded by

Marsha Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Module 1

Chapter 2-Classes and Objects

 Classes and Objects

Objects and classes are used to wrap related functions and data in one place in C++.

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 calculate_area() and calculate_volume().

However, in C++, rather than creating separate variables and functions, we can also wrap the related
data and functions in a single place (by creating objects).

This programming paradigm is known as object-oriented programming.

 Create a Class

A class is defined in C++ using the keyword class followed by the name of the class.

The body of the class is defined inside 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 calculate_area()
{
return length * breadth;
}

double calculate_volume()
{
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 calculate_area() and calculate_volume () are known as member functions of a
class.
Syntax to Define Object in C++

ClassName object_name;

We can create objects of Room class (defined in the above example) as follows:

// sample function

void sample_function()
{
// 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 sample_function().
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.calculate_area();
This will call the calculate_area() 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.
Example

#include <iostream>
class car
{
private:
int car_number;
char car_model[10];
public:
void getdata()
{
cout<<"Enter car number: "; cin>>car_number;
cout<<"\n Enter car model: "; cin>>car_model;
}
void showdata()
{
cout<<"Car number is "<<car_number;
cout<<"\n Car model is "<<car_model;
}
};
// main function starts
int main()
{
car c1;
c1.getdata();
c1.showdata();
return 0;
}

Here in above program getdata() and showdata() are the member function defined inside the class.

Program to demonstrate outside the class


#include <iostream>
class car
{
private:
int car_number;
char car_model[10];
public:
void getdata(); //function declaration
void showdata();
};
// function definition
void car::getdata()
{
cout<<"Enter car number: "; cin>>car_number;
cout<<"\n Enter car model: "; cin>>car_model;
}
void car::showdata()
{
cout<<"Car number is "<<car_number;
cout<<"\n Car model is "<<car_model;
}
// main function starts
int main()
{
car c1;
c1.getdata();
c1.showdata();
return 0;
}
Friend Class and Function
Program to demonstrate Friend class

#include <iostream.h>
#include <conio.h>

class GFG {
private:
int private_variable;

protected:
int protected_variable;

public:
GFG()
{
private_variable = 10;
protected_variable = 99;
}

// friend class declaration


friend class F;
};

// Here, class F is declared as a


// friend inside class GFG. Therefore,
// F is a friend of class GFG. Class F
// can access the private members of
// class GFG.
class F {
public:
void display(GFG& t)
{
cout << "The value of Private Variable = "
<< t.private_variable << endl;
cout << "The value of Protected Variable = "
<< t.protected_variable;
}
};

// Driver code
int main()
{
clrscr();
GFG g;
F fri;
fri.display(g);
getch();
return 0;
}
// C++ program to create a global function as a friend function of some class
#include <iostream.h>
class base {
private:
int private_variable;

protected:
int protected_variable;

public:
base()
{
private_variable = 10;
protected_variable = 99;
}

// friend function declaration


friend void friendFunction(base& obj);
};

// friend function definition


void friendFunction(base& obj)
{
cout << "Private Variable: " << obj.private_variable
<< endl;
cout << "Protected Variable: " << obj.protected_variable;
}

// driver code
int main()
{
base object1;
friendFunction(object1);

return 0;
}

// C++ program to create a member function of another class as a friend function


#include <iostream>
using namespace std;

class base; // forward definition needed


// another class in which function is declared
class anotherClass {
public:
void memberFunction(base& obj);
};

// base class for which friend is declared


class base {
private:
int private_variable;

protected:
int protected_variable;
public:
base()
{
private_variable = 10;
protected_variable = 99;
}

// friend function declaration


friend void anotherClass::memberFunction(base&);
};

// friend function definition


void anotherClass::memberFunction(base& obj)
{
cout << "Private Variable: " << obj.private_variable
<< endl;
cout << "Protected Variable: " << obj.protected_variable;
}

// driver code
int main()
{
base object1;
anotherClass object2;
object2.memberFunction(object1);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy