Class Name
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.
Classes are a fundamental concept in Object-Oriented Programming (OOP).
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:
● Data members (variables).
● Member functions (methods)
class ClassName
public: // Access specifier
int attribute; // Data members (attributes)
void display() // Member functions (methods)
{
cout << "Attribute: " << attribute << std::endl;
} };
Inside the Class Definition
#include <iostream> void display()
using namespace std; {
class student { cout<<"roll no is"<<rollno;
public: cout<<"name is"<<name;
int rollno; }
char name[10]; };
int main()
void get() {
{ student s1;
cout<<"enter roll no"; s1.get();
cin>> rollno; s1.display();
cout<<" enter name"; return 0;
cin>>name; }
}
Outside the class definition
#include <iostream> void student:: display()
using namespace std; {
class student { cout<<"roll no is"<<rollno;
public: cout<<"name is"<<name;
int rollno; }
char name[10]; int main()
{
void get() student s1;
{ s1.get();
cout<<"enter roll no"; s1.display();
cin>> rollno; return 0;
cout<<" enter name"; }
cin>>name;
}
void display();
};
Access Specifiers in C++
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.
#include<iostream> // main function
using namespace std; int main()
// class definition {
class Circle Circle obj;
{
public: // accessing public datamember outside class
double radius; obj.radius = 5.5;
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.
#include<iostream> int main()
using namespace std; {
// creating object of the class
class Circle Circle obj;
{
// private data member // trying to access private data member
private: // directly outside the class
double radius; obj.radius = 1.5;
// public member function cout << "Area is:" << obj.compute_area();
public: return 0;
double compute_area() }
{ // member function can access private
// data member radius
return 3.14*radius*radius;
}
};
#include<iostream> cout << "Radius is: " << radius << endl;
using namespace std; cout << "Area is: " << area;
}
class Circle
{ };
// private data member
private: // main function
double radius; int main()
{
// public member function // creating object of the class
public: Circle obj;
void compute_area(double r)
{ // trying to access private data member directly
// member function can access private data member outside the class
radius obj.compute_area(1.5);
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.
#include <iostream> void displayId()
using namespace std; {
class Parent cout << "id_protected is: " << id_protected <<
{ endl;
// protected data members }
protected: };
int id_protected;
// main function
}; int main() {
// sub class or derived class from public base class Child obj1;
class Child : public Parent // member function of the derived class can
{ // access the protected data members of the base
public: class
void setId(int id)
{ obj1.setId(81);
// Child class is able to access the inherited obj1.displayId();
// protected data members of base class return 0;
id_protected = id; } }
Summary
Access Levels in Inheritance
Passing Object as an Argument
To pass an object as an argument we write the object name as the argument while calling the function
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();
int main() { return 0;
// object declarations }
Demo d1;
Demo d2;
Demo d3;
Returning objects from functions
Returning an Object by Value-When returning an object by value, a copy of the object is created and returned to the caller.
#include <iostream> // print member variables of Student
using namespace std; cout << "Age of Student 1 = " << student.age1 <<
class Student { endl;
public:
int age1, age2;
}; cout << "Age of Student 2 = " << student.age2 <<
// function that returns object of Student endl;
Student newStudent() {
Student student; return student;
}
// Initialize member variables of Student int main() {
student.age1 = 10; Student student1;
student.age2 = 20;
student1 = newStudent(); // Call function
return 0;
}
Friend class
A friend class can access both private and protected members of the class in which it has been declared as
friend.
#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.
Declaration of friend function in C++
class class_name
{
friend data_type function_name(argument/s); // syntax of friend function.
};
Characteristics of a Friend function:
○ 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:
friend int printLength(Box); //friend function
};
int printLength(Box b)
{
b.length = 10;
return b.length;
}