OOP Full Notes
OOP Full Notes
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.
int main() {
cout << "Hello World!"; // Output
return 0;
}
Page 1
Created by Turbolearn AI
Applications of OOP
Page 2
Created by Turbolearn AI
Principles of OOP
Page 3
Created by Turbolearn AI
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.
Class Structure:
class class_name {
private:
// Private declarations
public:
// Public declarations
};
Page 4
Created by Turbolearn AI
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.
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
// ...
};
Arrays
Page 5
Created by Turbolearn AI
Example:
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();
};
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.
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.
Page 11