Unit I
Unit I
Class, Object:-
A class is a blueprint for creating objects and defines both the data members and
member functions that characterize an object. An object is an instance of a class that contains
actual values for the class attributes. For example, a Student class can be defined with data
members such as name and roll number and a member function to display these values. In C+
+, this can be implemented as follows:
#include <iostream>
using namespace std;
class Student {
public:
string name;
int rollNo;
void display() {
cout << "Name: " << name << ", Roll No: " << rollNo << endl;
}
};
int main() {
Student s1;
[Link] = "Alice";
[Link] = 101;
[Link]();
return 0;
}
Encapsulation:-
Example: Using private for data and public functions to access it.
This example shows how a class serves as a template and an object represents a
specific entity created from that template. Encapsulation, a key principle of object-oriented
programming, allows data members to be hidden and accessed only through public member
functions. For instance, a BankAccount class may have a private balance member and public
methods such as deposit and getBalance:
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance;
public:
void deposit(double amt) { balance += amt; }
double getBalance() { return balance; }
};
int main() {
BankAccount acc;
[Link](5000);
cout << "Balance: " << [Link]() << endl;
return 0;
}
Balance: 5000
Abstraction
Abstraction hides unnecessary internal details and exposes only essential features to
the user. This can be implemented in C++ using abstract classes and pure virtual functions.
For example, a Shape class may have a pure virtual function draw(), which is implemented
by a derived Circle class:
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0;
};
int main() {
Circle c;
[Link]();
return 0;
}
Drawing Circle
Inheritance :-
Inheritance allows a derived class to acquire the properties and behaviors of a base
class, promoting code reuse and hierarchical modeling. For example, a Vehicle class may
contain a speed member, and a Car class can inherit from Vehicle and add a doors member.
The following C++ program demonstrates single inheritance:
#include <iostream>
using namespace std;
class Vehicle {
public:
int speed;
};
int main() {
Car c;
[Link] = 120;
[Link] = 4;
[Link]();
return 0;
}
Polymorphism:-
#include <iostream>
using namespace std;
int main() {
cout << add(5, 3) << endl;
cout << add(2.5, 3.5) << endl;
return 0;
}
8
6
Run-time polymorphism:-
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() { cout << "Base" << endl; }
};
int main() {
Base* b;
Derived d;
b = &d;
b->show();
return 0;
}
The output is:
Derived
Features of OOPS
Constructors:-
Constructors are special member functions that initialize objects automatically when
they are created. They can be default, parameterized, or copy constructors. Destructors are
automatically called when objects go out of scope to perform cleanup. An example
demonstrating constructors and destructors:
#include <iostream>
using namespace std;
class Student {
string name;
public:
Student(string n) { name = n; cout << name << " object created" << endl; }
~Student() { cout << name << " object destroyed" << endl; }
};
int main() {
Student s1("Alice");
Student s2("Bob");
}
#include <iostream>
using namespace std;
class Vehicle {
protected:
int speed;
public:
Vehicle(int s) { speed = s; }
virtual void show() { cout << "Vehicle speed: " << speed << endl; }
};
int main() {
Vehicle* v;
Car c(120, 4);
v = &c;
v->show();
return 0;
}
Destructor :-
For example, consider a Student class that demonstrates the creation and destruction of
objects:
#include <iostream>
using namespace std;
class Student {
string name;
public:
Student(string n) {
name = n;
cout << name << " object created" << endl;
}
~Student() {
cout << name << " object destroyed" << endl;
}
};
int main() {
Student s1("Alice"); // Constructor called
Student s2("Bob"); // Constructor called
// Destructor automatically called when objects go out of scope
return 0;
}
In this program, when the objects s1 and s2 are created, the constructor is automatically
invoked, initializing the object and displaying a message that the object has been created.
When the main function ends, both s2 and s1 go out of scope, and the destructor is
automatically called for each object in the reverse order of creation. This ensures that
resources associated with the objects are properly released. The sample output of the program
is:
Friend Function
Definition
A friend function in C++ is a function that is not a member of a class, but it is permitted to
access the private and protected members of that class by using the keyword friend.
Key Points
Example Program
#include <iostream>
using namespace std;
class Sample {
private:
int a;
public:
Sample(int x) {
a = x;
}
void display(Sample s) {
cout << "Value of a: " << s.a << endl;
}
int main() {
Sample obj(10);
display(obj);
return 0;
}
Function Overloading
Definition
Function overloading is a feature of C++ that allows more than one function with the
same name but with different number or types of parameters.
Key Points
Example Program
#include <iostream>
using namespace std;
int main() {
cout << "Addition of two integers: " << add(10, 20) << endl;
cout << "Addition of two floats: " << add(2.5f, 3.5f) << endl;
cout << "Addition of three integers: " << add(1, 2, 3) << endl;
return 0;
}
Operator Overloading
Definition
Example Program
#include <iostream>
using namespace std;
class Complex {
int real, imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(2, 3), c2(4, 5);
Complex c3 = c1 + c2;
[Link]();
return 0;
}
Example Program
#include <iostream>
using namespace std;
class Complex {
int real, imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(1, 2), c2(3, 4);
Complex c3 = c1 + c2;
[Link]();
return 0;
}
Short Comparison
Topic Purpose