0% found this document useful (0 votes)
50 views11 pages

Unit I

This document provides an introduction to Object-Oriented Programming (OOP), comparing it with procedural programming and outlining key features such as classes, objects, encapsulation, abstraction, inheritance, and polymorphism. It includes examples of constructors, destructors, friend functions, function overloading, and operator overloading in C++. The document emphasizes the benefits of OOP in creating modular, reusable, and secure code.

Uploaded by

sangeethamtech26
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views11 pages

Unit I

This document provides an introduction to Object-Oriented Programming (OOP), comparing it with procedural programming and outlining key features such as classes, objects, encapsulation, abstraction, inheritance, and polymorphism. It includes examples of constructors, destructors, friend functions, function overloading, and operator overloading in C++. The document emphasizes the benefits of OOP in creating modular, reusable, and secure code.

Uploaded by

sangeethamtech26
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

UNIT-I INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING

Procedural vs. Object-Oriented Programming- Features of OOPS -Class, Object,


Encapsulation, Abstraction, Inheritance, Polymorphism- - Constructors and Destructors-
Friend Functions - Function and Operator Overloading

Procedural vs. Object-Oriented Programming:-

Object-oriented programming is a programming paradigm that emphasizes modelling


real-world entities as objects which combine both data and the functions that operate on that
data. Unlike procedural programming, which follows a top-down approach and focuses on
functions while keeping data separate, object-oriented programming follows a bottom-up
approach and focuses on creating reusable, modular, and secure code using classes and
objects. Procedural programming languages such as C and Pascal are simple for small
programs but become difficult to manage for large systems due to low reusability and less
secure data handling. Object-oriented programming languages such as C++, Java, and Python
provide encapsulation, inheritance, and polymorphism, allowing better structure, code reuse,
and flexibility. Encapsulation ensures that the internal data of an object is hidden and can
only be accessed through defined methods, providing data security and controlled access.

Feature Procedural Programming Object-Oriented Programming


Follows top-down, focuses on
Approach Follows bottom-up, focuses on objects
functions
Data and functions are encapsulated in
Data Handling Data is separate from functions
objects
High, through inheritance and
Reusability Low, functions may be reused
polymorphism
Security Less secure, direct access to data More secure, uses encapsulation
Examples C, Pascal C++, Java, Python

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;
}

The sample output of this program is:

Name: Alice, Roll No: 101

Encapsulation:-

 Definition: Wrapping data and functions together in a class.

 Purpose: Data hiding and access control.

 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;
}

The output of this program is:

Balance: 5000

Abstraction

 Definition: Hiding internal details and showing only necessary features.

 Purpose: Simplifies complex systems and reduces programming errors.

 Example: Function prototypes, abstract classes, or interfaces in C++.

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;
};

class Circle : public Shape {


public:
void draw() {
cout << "Drawing Circle" << endl;
}
};

int main() {
Circle c;
[Link]();
return 0;
}

The output of this program is:

Drawing Circle

Inheritance :-

 Definition: Mechanism where a class (child/derived) acquires properties of another


class (parent/base).
 Purpose: Reusability of code.
 Example:
class Vehicle { public: int speed; };
class Car : public Vehicle { public: int doors; };

Types: Single, Multilevel, Multiple, Hierarchical, Hybrid.

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;
};

class Car : public Vehicle {


public:
int doors;
void display() {
cout << "Speed: " << speed << ", Doors: " << doors << endl;
}
};

int main() {
Car c;
[Link] = 120;
[Link] = 4;
[Link]();
return 0;
}

The output is:

Speed: 120, Doors: 4

Polymorphism:-

 Definition: Ability of a function or operator to behave differently based on context.


 Types:
o Compile-time (Overloading): Function overloading, operator overloading
o Run-time (Overriding): Virtual functions

Example (Function Overloading):

int add(int a, int b);


double add(double a, double b);
Polymorphism allows functions, methods, or operators to behave differently based on
the context. Compile-time polymorphism can be achieved through function overloading and
operator overloading. For example:

#include <iostream>
using namespace std;

int add(int a, int b) { return a + b; }


double add(double a, double b) { return a + b; }

int main() {
cout << add(5, 3) << endl;
cout << add(2.5, 3.5) << endl;
return 0;
}

The output is:

8
6

Run-time polymorphism:-

Example (Run-time Polymorphism):

class Base { virtual void show() { cout << "Base"; } };


class Derived : public Base { void show() { cout << "Derived"; } };

Run-time polymorphism is achieved using virtual functions. A base class pointer


pointing to a derived class object can call an overridden virtual function in the derived class:

#include <iostream>
using namespace std;

class Base {
public:
virtual void show() { cout << "Base" << endl; }
};

class Derived : public Base {


public:
void show() { cout << "Derived" << endl; }
};

int main() {
Base* b;
Derived d;
b = &d;
b->show();
return 0;
}
The output is:

Derived

Features of OOPS

[Link] Feature Definition Example / Description


A class is a blueprint or template used to
1 Class class Student { };
create objects.
An object is an instance of a class and
2 Object Student s1;
represents real-world entities.
Binding data and functions into a single unit Private data with public
3 Encapsulation
and providing data hiding. methods
Data Showing only essential details and hiding Function declaration
4
Abstraction implementation details. without definition
One class acquires properties of another class
5 Inheritance class B : public A { };
for reusability.
One name, many forms; same
6 Polymorphism Function overloading
function/operator behaves differently.
Dynamic
7 Function call is resolved at runtime. Virtual functions
Binding
Message
8 Objects communicate through function calls. [Link]();
Passing

Constructors:-

 Definition: Special member function used to initialize objects.

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");
}

The output is:


Alice object created
Bob object created
Bob object destroyed
Alice object destroyed

A combined example demonstrating encapsulation, inheritance, polymorphism, and


constructors is as follows. A Vehicle class contains a speed member, and a Car class inherits
Vehicle and adds a doors member. Using a base class pointer, run-time polymorphism is
demonstrated with a virtual show() function:

#include <iostream>
using namespace std;

class Vehicle {
protected:
int speed;
public:
Vehicle(int s) { speed = s; }
virtual void show() { cout << "Vehicle speed: " << speed << endl; }
};

class Car : public Vehicle {


int doors;
public:
Car(int s, int d) : Vehicle(s) { doors = d; }
void show() { cout << "Car speed: " << speed << ", Doors: " << doors << endl; }
};

int main() {
Vehicle* v;
Car c(120, 4);
v = &c;
v->show();
return 0;
}

The output is:

Car speed: 120, Doors: 4

Destructor :-

A destructor is a special member function in C++ that is automatically called when an


object goes out of scope or is explicitly deleted. Unlike constructors, destructors do not take
any arguments and do not have a return type. The primary purpose of a destructor is to
release resources allocated to an object during its lifetime, such as dynamic memory, file
handles, or network connections, and to perform any necessary cleanup operations. In C++,
the destructor has the same name as the class, but it is preceded by a tilde (~). Each class can
have only one destructor, and it cannot be overloaded or called explicitly like a regular
function, although it can be invoked automatically when the object is destroyed.

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:

Alice object created


Bob object created
Bob object destroyed
Alice object destroyed

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

 Declared inside the class using friend keyword


 Defined outside the class
 Called like a normal function
 Helps in sharing data between classes

Example Program
#include <iostream>
using namespace std;

class Sample {
private:
int a;
public:
Sample(int x) {
a = x;
}

friend void display(Sample s);


};

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

 Improves code readability


 Functions must differ in parameter list
 Return type alone cannot overload functions

Example Program
#include <iostream>
using namespace std;

int add(int a, int b) {


return a + b;
}

float add(float a, float b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}

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

Operator overloading is a technique in C++ that allows programmers to redefine the


meaning of operators (like +, -, *) for user-defined data types.

(a) Operator Overloading using Member Function

Example Program
#include <iostream>
using namespace std;

class Complex {
int real, imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}

Complex operator + (Complex c) {


Complex temp;
[Link] = real + [Link];
[Link] = imag + [Link];
return temp;
}

void display() {
cout << real << " + " << imag << "i" << endl;
}
};

int main() {
Complex c1(2, 3), c2(4, 5);
Complex c3 = c1 + c2;
[Link]();
return 0;
}

(b) Operator Overloading using Friend Function

Example Program
#include <iostream>
using namespace std;

class Complex {
int real, imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}

friend Complex operator + (Complex c1, Complex c2);

void display() {
cout << real << " + " << imag << "i" << endl;
}
};

Complex operator + (Complex c1, Complex c2) {


Complex temp;
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];
return temp;
}

int main() {
Complex c1(1, 2), c2(3, 4);
Complex c3 = c1 + c2;
[Link]();
return 0;
}

Short Comparison

Topic Purpose

Friend Function Access private data of a class

Function Overloading Same function name with different parameters

Operator Overloading Redefine operators for objects

You might also like