Chapter 2:
Diffrence between class and object
class object
A class is a blueprint or template for creating An object is an instance of a class.
objects.
No memory is allocated when a class is defined. Memory is allocated when an object is created.
Defines properties (data members) and Represents a real-world entity with actual values
behaviors (member functions). for data members.
Defined using the class keyword. Created using the class name followed by an
object name.
Class is logical entity Object is physical entity
Uses private, protected, and public access Follows access rules defined by the class.
specifiers.
One class can define multiple objects. Each object has its own separate memory and
values.
Exists until the program is compiled. An object exists until it goes out of scope or is
deleted.
Example: class Car { public: int speed; void Example:Car myCar; myCar.speed = 100;
drive() {} }; myCar.drive();
Scope Resolution Operator in C++
In C++, the scope resolution operator (::) is used to access the identifiers such as variable
names and function names defined inside some other scope in the current scope.
#include <iostream>
int main() {
// Accessing cout from std namespace using scope
// resolution operator
std::cout << "Hello";
return 0;
}
Applications of Scope Resolution Operator
Following are the main applications of scope resolution operator illustrated with an
example:
1. Accessing Global Variables
When a local variable shadows a global variable, we can use :: to access the global
variable.
#include <iostream>
using namespace std;
// Global x
int x = 3;
int main() {
// Local x
int x = 10;
// Printing the global x
cout << ::x;
return 0;
2. Define Class Member Function Outside Class
It is also used to define the member function of the class outside the class template.
#include <iostream>
using namespace std;
// A sample class
class A {
public:
// Only declaration of member function
void fun();
};
// Definition outside class by referring to it
// using ::
void A::fun() {
cout << "fun() called";
int main() {
A a;
a.fun();
return 0;
3. Access Class’s Static Members
Static members of a class can be accessed without creating the object of the class. It is
possible to access them using scope resolution operator.
#include<iostream>
using namespace std;
class A {
public:
static int x;
};
// In C++, static members must be explicitly defined
// like this
int A::x = 1;
int main() {
// Accessing static data member
cout << A::x;
return 0;
4. Refer to Base Class Member in Derived Class
The scope resolution operator can also be used to refer to the members of base class in a
derived class especially if they have the same name
#include <bits/stdc++.h>
using namespace std;
class Base {
public:
void func() {
cout << "Base class func()" << endl;
};
class Derived : public Base {
public:
// Overridden function
void func() {
cout << "Derived class func()" << endl;
}
};
int main() {
Derived obj;
// Calling base class's func() from the object of
// derived class
obj.Base::func();
obj.func();
return 0;
Data handling
In Object-Oriented Programming (OOPs) refers to how data (attributes or member variables) is
stored, accessed, modified, and secured within classes and objects. C++ provides several
mechanisms for efficient data handling while maintaining encapsulation, security, and modularity.
Access specifiers control data access:
private: Accessible only inside the class.
protected: Accessible in derived classes.
public: Accessible anywhere.
How to create a class and object explain
A class is defined using the class keyword.
It contains data members (variables) and member functions (methods).
In C++, a class is a blueprint that defines attributes (data members) and behaviors (member
functions), while an object is an instance of a class that stores actual data and allows interaction
with methods.
Step 1: Define a Class
A class is defined using the class keyword.
It contains data members (variables) and member functions (methods).
class ClassName {
public:
// Data members (attributes)
int variable1;
// Member functions (methods)
void function1() {
// Function logic
}
};
Step 2: Create an Object
An object is an instance of a class.
It is created by specifying the class name followed by the object name.
ClassName objectName; // Creating an object
Example: Creating a Class and an Object
#include <iostream>
using namespace std;
// Defining a class
class Car {
public:
// Data members
string brand;
int speed;
// Member function
void showDetails() {
cout << "Car Brand: " << brand << ", Speed: " << speed << "
km/h" << endl;
}
};
int main() {
// Creating an object of class Car
Car myCar;
// Assigning values to object's attributes
myCar.brand = "Toyota";
myCar.speed = 150;
// Calling a member function
myCar.showDetails();
return 0;
}
Member Function in C++
A member function in C++ is a function defined inside a class and is used to manipulate
or access the class’s data members. It defines the behavior of an object.
Types of Member Functions in C++
1. Inside-Class Definition :
2. Outside-Class Definition
3. Inline Functions
4. Constant Member Functions
5. Static Member Functions
6. Friend Functions
7. Virtual Functions (for polymorphism)
Difference between constructor vs destructor.
Types of constructor:
Default Constructor :
A constructor without parameters.
Initializes an object with default values.
If no constructor is provided, C++ automatically generates a default constructor.
Parameterized Constructor :
A constructor that takes arguments to initialize an object.
Helps in assigning different values to different objects.
Copy Constructor
Creates a new object by copying an existing object.
Default copy constructor does a shallow copy.
Can be explicitly defined for deep copying.
Move Constructor (C++11)
Constructor with Default Arguments
Explicit Constructor
How to pass and return object from C++ Functions?
In C++ programming, we can pass objects to a function in the same way as passing
built-in data types.
// C++ program to calculate the average marks of two students
#include <iostream>
using namespace std;
class Student {
public:
double marks;
// constructor to initialize marks
Student(double m)
: marks{m} {
};
// function that has objects as parameters
double average_marks(Student s1, Student s2) {
// return the average of marks of s1 and s2
return (s1.marks + s2.marks)/2 ;
}
int main() {
Student student1(88.0), student2(56.0);
// pass the objects as arguments
cout << "Average Marks = " << average_marks(student1, student2) << "\n";
return 0;
Create a class and object
#include <iostream>
using namespace std;
class Vehicle {
public:
string brand;
void showBrand() {
cout << "Brand: " << brand << endl;
};
int main() {
Vehicle car; // Object creation
car.brand = "Royal Enfield";
car.showBrand();
return 0;
}
C++ Program: Calculate Area of a Circle
#include <iostream>
using namespace std;
class Circle {
private:
double radius;
public:
// Constructor to initialize radius
Circle(double r) {
radius = r;
// Function to calculate area
double getArea() {
return 3.14159 * radius * radius; // πr²
};
int main() {
double r;
cout << "Enter the radius of the circle: ";
cin >> r;
Circle c(r); // Create Circle object with user input
cout << "Area of the Circle: " << c.getArea() << endl;
return 0;
Friend function:
A friend function in C++ is a function that is not a member of a class but has access to its
private and protected members. It is declared inside the class with the friend keyword.
Example of a Friend Function in C++:
#include <iostream>
using namespace std;
class Vehicle {
private:
string brand;
public:
Vehicle(string b) {
brand = b;
// Friend function declaration
friend void showBrand(Vehicle v);
};
// Friend function definition
void showBrand(Vehicle v) {
cout << "Brand: " << v.brand << endl;
int main() {
Vehicle bike("Royal Enfield");
showBrand(bike); // Calling friend function
return 0;