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

OOP Full Notes

This document provides an overview of Object Oriented Programming (OOP) with C++, covering its principles, history, program structure, and advantages over procedural languages. It explains key concepts such as classes, objects, constructors, destructors, and inheritance, along with their applications in various fields. The document also highlights access specifiers and the types of inheritance in C++.

Uploaded by

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

OOP Full Notes

This document provides an overview of Object Oriented Programming (OOP) with C++, covering its principles, history, program structure, and advantages over procedural languages. It explains key concepts such as classes, objects, constructors, destructors, and inheritance, along with their applications in various fields. The document also highlights access specifiers and the types of inheritance in C++.

Uploaded by

bollysony152
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Created by Turbolearn AI

Object Oriented Programming with C++


Introduction to Object Oriented Programming
(OOP)
OOP is a problem-solving approach where computations are performed using
objects.
It prioritizes data over procedures.
It follows a bottom-up approach, with the main method at the end calling class
objects.
Easily add new data and functions.
Provides enhanced system security.
Programs are reusable and extensible.

History of C++
Invented by Bjarne Stroustrup at AT&T Bell Labs in 1979.
Initially called "C with Objects," later renamed to C++ in 1983.
Evolved from C (a procedural language) into an object-oriented language.
Classes are called using objects.

Basic C++ Program Structure

#include <iostream.h> // Header file for input/output


#include <conio.h> // For console input/output

int main() {
cout << "Hello World!"; // Output
return 0;
}

iostream.h: Standard input/output stream.


cout: Character output.
cin: Character input.
\n or endl: Newline character.

Advantages of Procedural Languages

Page 1
Created by Turbolearn AI

Easy to break down large tasks into smaller functions.


Code reusability through functions.
Clear process flow.
Simple to learn, suitable for small projects.
Fast and efficient program execution.

Disadvantages of Procedural Languages


Difficult to manage large programs.
Lack of data protection; any part of the program can modify data.
No inheritance.
Less focus on data organization and protection.
Not ideal for modeling real-world entities.

Applications of OOP

Page 2
Created by Turbolearn AI

Application Area Examples Description

Mobile apps, games, Creates user interface elements


User-friendly apps
web browsers (buttons, windows) as objects.
Models real-world items as objects for
Train ticket booking,
Real-world systems easier management of complex
traffic systems
systems.
Objects can exist on different
Cloud & networked Cloud storage, online
computers for internet-based
applications games
applications.
Weather forecasting, Simulates real-world objects to predict
Simulation
business simulations outcomes.
Uses objects to represent data for
Database Online shopping sites,
efficient storage and management of
Management banking systems
large datasets.
Architecture design, 3D Models parts as objects to create
Software Design
modeling tools complex designs.
Artificial Voice assistants, image Uses objects to represent different
Intelligence (AI) recognition aspects of AI systems.
Business Inventory systems, Organizes data (customers, products)
Applications customer management into objects.
Creates game elements (characters,
Game Development Video games
weapons) as objects.
Password management, Manages users and access rights using
Security Systems
fingerprint scanners objects.
Online stores, social Represents users, posts, products as
Websites
media sites objects.

Principles of OOP

Page 3
Created by Turbolearn AI

Classes: Blueprints for creating objects.

Data Encapsulation: Bundling data and methods that operate on that data
within a class. Protecting data from outside access.

Data Abstraction: Showing only essential information to the user and hiding
unnecessary details.

Inheritance: Creating new classes (derived classes) from existing classes (base
classes), inheriting properties and behaviors.

Polymorphism: The ability of an object to take on many forms. This allows you
to use the same method name for different classes, each implementing the
method in their own specific way.

Function (method) overloading: Using the same function name for


different tasks.
Dynamic Binding (Late Binding): Determining the method to execute at
runtime.

Classes and Objects


A class is a blueprint for creating objects. It defines the data (member
variables) and the functions (member methods) that operate on that
data. Access to member variables is controlled using access specifiers
(private, public, protected).

An object is an instance of a class. It represents a specific entity that has


the characteristics defined by the class.

Class Structure:

class class_name {
private:
// Private declarations
public:
// Public declarations
};

Page 4
Created by Turbolearn AI

Private: Members accessible only within the class.


Public: Members accessible from outside the class.
Protected: Similar to private, but accessible by derived classes as well (covered
in inheritance).

Access Specifiers
Private: Restricts access to members within the class. Default access level.
Public: Allows access to members from anywhere.
Protected: Access restricted to members of the class and its derived classes.

Objects and Class Function Calls


Objects are created using the class name followed by the object name.
Class functions are called using the dot (.) operator.

class_name object;
object.myFunction();

Friend Functions
A friend function is declared within a class using the friend keyword. It
can access the class's private and protected members, even though it is
not a member function of the class.

Syntax:

class ClassName {
private:
// ...
friend void friendFunction(ClassName); // Declaration of friend function
// ...
};

Advantages: Useful in operator overloading and when inter-class relationships


exist.
Disadvantages: Not inherited.

Arrays

Page 5
Created by Turbolearn AI

An array is a linear data structure that stores a collection of elements of


the same data type. Elements are accessed using their index (starting
from 0).

Example:

int myArray[3]; // Declares an array of 3 integers


myArray[0] = 10;
myArray[1] = 15;
myArray[2] = 20;

Advantages: Easy element access and searching.


Disadvantages: Fixed size; cannot be resized after creation.

Constructors and Destructors


A constructor is a special method with the same name as the class. It is
automatically called when an object of the class is created. It is used to
initialize the object's data members.

A destructor is a special method with the same name as the class,


preceded by a tilde (~). It is automatically called when an object is
destroyed. It is used to perform cleanup tasks.

Default Constructor: No parameters.


Parameterized Constructor: Takes parameters for initialization.
Constructor Overloading: Multiple constructors with different parameters.

Default Constructor
A default constructor in C++ is automatically called when an object is created. It
takes no arguments and has no parameters.

Page 6
Created by Turbolearn AI

class Test{
public:
Test(){
cout<<“default constructor”;
}
};
void main(){
Test t;
}

Parameterized Constructor ‍
A parameterized constructor has parameters that accept arguments when an object
of the class is created. If values aren't passed when creating an object, it will throw
an error. These are useful for constructor overloading.

#include<iostream.h>
#include<conio.h>
class Exam{
private:
int seats;
public:
Exam(int s){
seats=s;
cout<<“Total number of seats in each class is”<<seats;
}
};
void main(){
clrscr();
Exam obj(33);
getch();
}

Destructors
A destructor has the same name as the class but with a tilde (~) before it. It cannot
be inherited or overloaded, but both derived and base classes can have their own. It
has no parameters or return type (not even void). It releases the memory occupied by
objects when a constructor is created. It cannot be directly called; the compiler
generates the call.

Page 7
Created by Turbolearn AI

class Student{
int roll_no, marks;
public:
~Student();
};

Constructors vs. Destructors


Feature Constructors Destructors

Overloading Can be overloaded Cannot be overloaded


Inheritance Can be inherited Cannot be inherited
Automatically called when an
Invocation Called by the compiler
object is created
Releases memory occupied by
Purpose Creates an object
the object
Declaration
No special symbol required Tilde (~) symbol is used
Symbol

Calling Derived Class Constructors


When a derived class object is created, the base class constructor is executed first,
then the derived class constructor. If the base class constructor has parameters, it
must be called separately. If there are multiple base classes (e.g., A inherits from B, C
inherits from B), constructors are called in the order of inheritance (A, then B, then C).

Page 8
Created by Turbolearn AI

#include<iostream.h>
#include<conio.h>
class Base{
public:
Base(){
cout<<“Base class Constructor”;
}
};
class Derived: public Base{
public:
Derived(){
cout<<“Derived class Constructor”;
}
};
void main(){
clrscr();
Derived obj;
getch();
}

Constructor Overloading
Constructor overloading means having multiple constructors in the same class with
different parameters or parameter types. The constructor called depends on the
arguments passed.

Page 9
Created by Turbolearn AI

#include<iostream.h>
#include<conio.h>
class College{
public:
College(){
cout<<"\n Default constructor with NO parameter";
}
College(int year){
cout<<"\n Parameterized constructor with 1 parameter";
cout<<"\n Welcome to the fest of "<<year;
}
College(int year, char grade){
cout<<"\n Parameterized constructor with 2 parameter";
cout<<"\n Year of college establishment"<<year;
cout<<"\n NAAC grade "<<grade;
}
};
int main(){
clrscr();
College tsdc1;
College tsdc2(2025);
College tsdc3(1992,'A');
getch();
return 0;
}

Inheritance in C++
Inheritance allows a class (derived class) to inherit properties and methods from
another class (base class). It's done using the colon (:) operator. The visibility-mode
(public, protected, or private) specifies the access level of inherited members.

Access Specifier Same Class Derived Class Outside Class

Public Yes Yes Yes


Protected Yes Yes No
Private Yes No No

Tips for Inheritance:

Keep base class members public or protected for inheritance.


Use public inheritance mode for easy access to base class methods.
Keep derived class members public for accessibility.

Types of Inheritance

Page 10
Created by Turbolearn AI

Single Inheritance: One derived class inherits from one base class.
Multiple Inheritance: One derived class inherits from multiple base classes.
Multilevel Inheritance: A derived class inherits from another derived class.
Hierarchical Inheritance: Multiple derived classes inherit from a single base
class.
Hybrid Inheritance: A combination of two or more inheritance types.

Examples of Inheritance Types


(Due to length constraints, detailed code examples for each inheritance type are
omitted but can be easily found in standard C++ textbooks or online resources.
The lecture transcript provided examples of each, but they're too lengthy to
reproduce effectively here.)

Access Specifiers in Inheritance


Public Inheritance: Public members remain public, protected members remain
protected.
Protected Inheritance: Public and protected members become protected in the
derived class.
Private Inheritance: Public and protected members become private in the
derived class.

Page 11

You might also like