Introduction to C++
and Object-Oriented
Programming
MODULE-5
Introduction to C++ and Object-Oriented Programming
• C++ is a general-purpose programming language developed by
Bjarne Stroustrup at Bell Labs in the early 1980s.
• It is an extension of C that introduces Object-Oriented Programming
(OOP) features while keeping the efficiency and flexibility of C.
• C++ is often called a multi-paradigm language because it supports:
Procedural programming (like C)
Object-Oriented programming (classes, objects)
Generic programming (templates)
• It is widely used for system software, game development, GUI
applications, embedded systems, and large-scale enterprise
software.
Key Features of C++
• Object-Oriented: Focuses on objects and data rather than just
functions.
• Encapsulation & Abstraction: Data and methods are wrapped
inside objects for security and clarity.
• Inheritance: Ability to derive new classes from existing ones.
• Polymorphism: Functions and operators can behave differently
based on context.
• Strong Type Checking: Detects errors at compile time.
• Standard Template Library (STL): Offers reusable classes and
functions for data structures and algorithms.
• Compatibility with C: Most C code can run in C++ program
Procedural Programming vs Object-Oriented Programming
Procedural Programming Object-Oriented Programming
Aspect
(PP) (OOP)
Program is divided into Program is divided into objects;
Approach functions; focuses on focuses on data and behavior
procedure/logic. together.
Data is global and shared Data is encapsulated within
Data Handling
among functions; less secure. objects; high security.
Code reusability is limited; Promotes high reusability through
Reusability functions are reused but not classes, inheritance, and
data structures. polymorphism.
Functions are separate but data
Classes combine data and
Modularity is not tied to them; low
methods; high modularity.
modularity.
Examples C, Pascal, Fortran C++, Java, Python, C#
.
.
Implementation of a Class in C++
• A class in C++ is a blueprint or template for creating objects.
• It defines data members (variables) and member functions
(methods) that operate on that data.
• An object is an instance of a class.
Syntax of a Class:
class ClassName {
// Access specifiers: public, private,
protected
public:
// data members
// member functions
};
Operations on Objects in C++
• An object is a real-world entity created from a class.
• A class is just a blueprint, and objects are instances of that
blueprint.
• Operations on objects include creation, initialization,
accessing members, passing/returning objects, copying,
and destruction.
Common Operations on Objects
Operation Description Example
Declaring an object of a
Creation Student s1;
class.
Using the dot (.) operator
[Link](101,
Accessing Members to access methods or 95.5);
attributes.
Setting values for data
Initialization members, usually via Student s2(102, 90);
constructors.
Sending objects to functions
Passing Objects as parameters (by value or displayStudent(s1);
reference).
A function can create and
Returning Objects Student createStudent();
return an object.
Use of a copy constructor
Copying Objects to create a new object from Student s3 = s1;
an existing one.
Objects are destroyed
automatically when they go
Destruction ~Student();
Example 1: Creating and Accessing Objects
Program:
#include <iostream>
Output:
using namespace std;
class Student {
private:
int rollNo; Roll No: 101, Marks: 92.5
float marks;
public:
void setDetails(int r, float m) {
rollNo = r;
marks = m; }
void display() {
cout << "Roll No: " << rollNo << ", Marks: " <<
marks << endl;
}
};
int main() {
Student s1; // Object creation
[Link](101, 92.5); // Accessing members
[Link]();
return 0;
Example 2: Passing and Returning Objects
Program:
#include <iostream> Output:
using namespace std;
class Box {
int l, w;
public:
2x4
void set(int x, int y) { l = x; w = y; }
5 x 10
void show() { cout << l << " x " << w <<
endl; }
};
void print(Box b) { [Link](); } // Pass object
Box create() { Box b; [Link](5, 10); return b; } //
Return object
int main() {
Box b1; [Link](2, 4);
print(b1); // Passing object
Box b2 = create();
[Link](); // Returned object
}
Example 3: Copying Objects (Copy Constructor)
Program: Output:
#include <iostream>
using namespace std;
class Student {
private:
Roll No: 101
int rollNo;
Roll No: 101
public:
Student(int r) { rollNo = r; }
Student(const Student &s) { rollNo = [Link]; } //
Copy Constructor
void display() { cout << "Roll No: " << rollNo <<
endl; }
};
int main() {
Student s1(101);
Student s2 = s1; // Copying object
[Link]();
[Link]();
return 0;
}
Relationship Among Objects in C++
• Objects in Object-Oriented Programming often interact with
each other. These interactions define their relationships, which
help in building real-world models in software.
• Main Types of Relationships:
Relationship Meaning Example
A general relationship where
A teacher teaches
Association objects know each other and can
students.
communicate.
"Has-a" relationship where one
A department has
Aggregation object contains another, but both
teachers.
can exist independently.
Strong "Has-a" relationship where
Composition one object is part of another and A car has an engine.
cannot exist independently.
"Is-a" relationship where a class
Inheritance A dog is an animal.
derives from another class.
Example: Association, Aggregation & Composition
Program:
#include <iostream> Output:
using namespace std;
class Engine {
public: void start() { cout << "Engine starts!\
n"; Engine starts!
} Car drives!
};
class Car { // Composition: Car "has"
Engine
Engine e;
public: void drive() { [Link](); cout << "Car
drives!\n";
}
};
int main() {
Car c; // Object of Car
[Link](); // Using Engine through Car
}
Specifying a Class in C++
• Specifying a class means defining the structure of a class in
C++.
• This includes data members (properties/attributes) and
member functions (methods/behaviors).
• It is like writing a blueprint before creating objects.
class ClassName {
Syntax:
// Access specifiers: private, public,
protected
private:
// data members (attributes)
public:
// member functions (behaviors)
};
Example:
Program: Output:
#include <iostream>
using namespace std;
class Student { // Class specification
private:
Roll: 101 Marks:
int roll; 95.5
float marks;
public:
void set(int r, float m) { roll = r; marks = m; }
void show() { cout << "Roll: " << roll << " Marks: " <<
marks << endl; }
};
int main() {
Student s; // Object creation
[Link](101, 95.5);
[Link]();
}
Creating Class Objects in C++
• A class is a blueprint; an object is an actual instance of that
class.
• Objects hold separate copies of data members but share the
same class definition.
• Objects are created after specifying a class.
Syntax:
ClassName objectName; // Single object
ClassName obj1, obj2, obj3; // Multiple
objects
Example:
Program: Output:
#include <iostream>
using namespace std;
class Student {
public:
Roll: 101
int roll; Roll: 102
void display() { cout << "Roll: " << roll
<< endl;
}
};
int main() {
Student s1, s2; // Creating two objects
[Link] = 101;
[Link] = 102;
[Link]();
[Link]();
}
Creating Class Methods in C++
• Methods are functions inside a class that define the behavior
of objects.
• They can be defined inside the class or outside using the
scope resolution operator ::
Syntax:
class ClassName {
public:
void methodName(); // Method declaration
};
void ClassName::methodName() { /*
definition */ }
Example:
Program:
#include <iostream> Output:
using namespace std;
class Student {
int roll;
public:
Roll: 101
void set(int r) { roll = r; } // Method
inside class
void show(); // Declared only
};
// Method defined outside class
void Student::show() { cout << "Roll: " <<
roll << endl; }
int main() {
Student s;
[Link](101);
[Link]();
}
Constructors in C++
• A constructor is a special member function of a class that is
automatically called when an object of the class is created. Its
main purpose is to initialize the objects of a class.
Key Features:
1. Same name as the class.
2. No return type (not even void).
3. Automatically invoked when an object is created.
4. Can be overloaded to allow different ways of initializing
objects.
Types of Constructors:
Type of Constructor Description Syntax / Example
Takes no arguments.
Automatically called to ClassName() { /*
Default Constructor
initialize objects with default initialization code */ }
values.
ClassName(type arg1,
Parameterized Takes arguments to initialize
type arg2, ...) { /*
Constructor objects with specific values. initialization code */ }
ClassName(const
Creates a new object as a
Copy Constructor ClassName &obj) { /*
copy of an existing object. copy code */ }
Parameterized Constructor
Program: Output:
#include <iostream>
using namespace std;
class Example {
public:
Example(int x) { Value: 10
cout << "Value: " << x <<
endl;
}
};
int main() {
Example obj(10); // Calls
parameterized constructor
return 0;
}
Default Constructor
Program: Output:
#include <iostream>
using namespace std;
class Example {
public: Default Constructor
Example() { called
cout << "Default
Constructor called\n";
}
};
int main() {
Example obj; // Automatically
calls default constructor
return 0;
}
Copy Constructor
Program: Output:
#include <iostream>
using namespace std;
class Example {
public: Copy Constructor called, data =
int data; 20
Example(int x) { data = x; }
Example(const Example &obj) {
data = [Link];
cout << "Copy Constructor called,
data = " << data << endl;
}
};
int main() {
Example obj1(20);
Example obj2 = obj1; // Calls copy
constructor
return 0;
}
Access Specifiers in C++
• Access specifiers define the accessibility of class members
(variables and functions) from outside the class.
Types of Access Specifiers:
Specifier Description Example
Members are accessible
public from anywhere in the public: int x;
program.
Members are accessible
private only within the class. private: int y;
Not accessible outside.
Members are accessible
within the class and
protected protected: int z;
derived classes
(inheritance).
.
Continued..
• Default Access Specifier:
1. In classes, default is private.
2. In structs, default is public
Example:
#include <iostream>
using namespace std;
class Example {
public:
int x; // Accessible anywhere
private:
int y; // Accessible only within class
protected:
int z; // Accessible in class & derived
classes
};
Example:
Program:
#include <iostream>
using namespace std;
class Base {
public:
int a = 10; // public Output:
private: 10 20 30
int b = 20; // private
protected: 10
int c = 30; // protected
public:
void show() {
cout << a << " " << b << " " << c << endl; }
};
int main() {
Base obj;
[Link](); // prints 10 20 30
cout << obj.a; // accessible
// cout << obj.b; // Error
// cout << obj.c; // Error
return 0;
}