OOP Module 1 Chapter 2
OOP Module 1 Chapter 2
Object Oriented
Programming Using C++
Module 1 Chapter 2
Classes and Objects
Book Genre Year Published
Student Name
Classes in C++
A class in C++ is a blueprint for creating objects. It encapsulates data (attributes) and functions (methods) that operate on that data into a single
entity.
Defining a Class
A class is defined using the class keyword, followed by the class name and a set of curly braces {}. Inside the class, you define:
class ClassName
Access specifiers in C++ define the scope and visibility of data members (variables) and member functions (methods) of a class. They determine
whether these members can be accessed directly or only through specific methods.
1. Public
● Members declared as public can be accessed from anywhere in the program, including outside the class.
● Commonly used for interface-like methods or data meant to be accessible to other parts of the program.
double compute_area() cout << "Radius is: " << obj.radius << "\n";
{ cout << "Area is: " << obj.compute_area();
return 3.14*radius*radius; return 0;
} }
};
2. Private
● Members declared as private can only be accessed by methods within the same class.
● They are not accessible directly from outside the class, even by derived classes.
● Used to implement encapsulation, hiding internal implementation details.
radius = r;
return 0;
double area = 3.14*radius*radius; }
3. Protected
● Members declared as protected can be accessed within the same class and by derived classes.
● They are not accessible from outside the class.
Syntax: function_name(object_name);
#include <iostream>
using namespace std; // assigning values to the data member of objects
d1.set(10);
class Demo { d2.set(20);
private:
int a; // passing object d1 and d2
d3.sum(d1, d2);
public:
void set(int x) { a = x; } // printing the values
void sum(Demo ob1, Demo ob2) { a = ob1.a + ob2.a; } d1.print();
void print() { cout << "Value of A : " << a << endl; } d2.print();
}; d3.print();
#include <iostream>
int main()
using namespace std; {
A a;
class A B b;
{ b.display(a);
int x =5; return 0;
friend class B; // friend class. }
};
class B
{
public:
void display(A &a)
{
cout<<"value of x is : "<<a.x;
}
};
Friend function
If a function is defined as a friend function in C++, then the protected and private data of a class can be accessed
using the function.
By using the keyword friend compiler knows the given function is a friend function.
For accessing the data, the declaration of a friend function should be done inside the body of a class starting with the
keyword friend.
○ The function is not in the scope of the class to which it has been declared as a friend.
○ It cannot be called using the object as it is not in the scope of that class.
○ It can be invoked like a normal function without using the object.
○ It cannot access the member names directly and has to use an object name and dot membership operator
with the member name.
○ It can be declared either in the private or the public part.
int main()
#include <iostream> {
using namespace std; Box b;
class Box cout<<"Length of box: "<< printLength(b)<<endl;
{ return 0;
private: }
int length;
public:
int printLength(Box b)
{
b.length = 10;
return b.length;
}