// base class
class Animal {
private :
int maxSpeed;
protected :
int numTyres;
public :
string color;
};
class Dog : public Animal {
// all things will be same;
};
class Dog : protected Animal {
// public will be protected also;
};
class Dog : private Animal {
// all will be private;
};
Inheritance : Order of Constructor/Destructor cal
#include <bits/stdc++.h>
using namespace std;
class Vehi{
private:
int maxSpeed;
protected:
int numT;
public:
string color;
// Vehi(){
// cout<<"its vehi 1constructor"<<endl;
// }
Vehi(int x, int y){
cout<< x <<endl;
cout<< y<<endl;
maxSpeed = x;
}
~Vehi(){
cout<<"its vehi distructor"<<endl;
}
};
class Car: public Vehi {
public:
int nG;
Car(int x, int y) : Vehi(x,y){
cout<< "car constructor"<<endl;
}
~Car(){
cout<< "car distructor"<<endl;
}
};
class HCar: public Car {
public:
HCar(int x,int y) : Car(x,y){
cout<< "H Hcar constructor"<<endl;
}
~HCar(){
cout<< "H Hcar1 distructor"<<endl;
}
};
int main()
{
// // Vehi v;
// // v.color="red";
// Car c(50);
// // c.color="blue";
// // c.nG=5;
HCar hc(10,20);
}
Inheritance : Types
#include <iostream>
using namespace std;
class Student {
public :
string name;
void print() {
cout << "Student " << endl;
}
};
class Teacher {
public :
string name;
string age;
void print() {
cout << "Teacher" << endl;
}
};
class TA : public Teacher, public Student {
public :
void print() {
cout << "TA " << endl;
}
};
int main() {
TA a;
// a.Student :: print();
// a.Teacher :: print();
a.print();
//===========>>>> a. className :: property name = value;
a.Teacher :: name = "abcd"; //a.name = "abc" won't work. bcz name was declare in Student and teacher. so I have to specifiy which name I'm calling. for this, a.Teacher :: name OR a.Student :: name !
}
Hybrid Inheritance
// jekhane age print pabe oitai print korbe. own class a print function thakle r parent er print function call korbe nah. jekhanei age print function pabe oi khanei age print kore dibe. r uporer print a jabe nah.
#include <iostream>
using namespace std;
class Vehicle {
public :
string color;
void print() {
cout << "Vehicle" << endl;
}
};
class Car : public Vehicle {
public :
int numGears;
void print() {
cout << "Car" << endl;
}
};
int main() {
Vehicle v;
Car c;
v.print();
c.print();
Vehicle *v1 = new Vehicle;
Vehicle *v2;
// v2 = &v;
v2 = &c;
v1 -> print();
v2 -> print(); // compile time Polymorphism
/*----------------------------------------*/
//funciton overloading => differnet funciton with same name
int test(int a, int b) {
}
int test(int a) {
}
int test() {
}
/*----------------------------------------*/
}
Polymorphism : Run time
#include <iostream>
using namespace std;
class Vehicle {
public :
string color;
// vehicle class dia Car class ke point korleo
//vehicle er print function call hoye jabe.
//tai Virtual add korle ami Car class er print function call korte parbo.
// but Car a kono print na thakle virtual Print call hobe.
virtual void print() {
cout << "Vehicle" << endl;
}
};
class Car : public Vehicle {
public :
int numGears;
void print() {
cout << "Car" << endl;
}
};
int main() {
Vehicle *v1 = new Vehicle;
v1 -> print();
// if no Print function in Car class then It will
//go to the Vehical class print funciton.
Car c;
Vehicle *v2;
v2 = &c;
v2 -> print();
}
//OUTPUT
Vehicle
Car
Virtual functions & Abstract classes
Pure function thaklei sheta Abstract class hoye jay. Pure function create kore Child class ke force kortesi oi function ta initialize korte. Example: amra Employee Class a calculateSalary Pure function create korbo then HR, SWE or other sector er class a calculateSalary function ke force korbo new kore calculateSalary function call korte or initialize korte.
// Pure virtual function
class Vehicle {
public :
string color;
// Pure virtual function
virtual void print() = 0; // pure function create an Abstract class
};
Friend Functions & Classes
#include <iostream>
using namespace std;
class Bus {
public :
void print();
};
void test();
class Truck {
private :
int x;
protected :
int y;
public :
int z;
friend class Bus; // friend class and and the class should be call before this class
friend void Bus :: print(); // friend function inside a Class and this Class+Function should be call before this class
friend void test(); // direct friend Function and the function should be call before this class
};
void Bus :: print() {
Truck t;
t.x = 10;
t.y = 20;
cout << t.x << " " << t.y << endl;
}
void test() {
// Access truck private
Truck t;
t.x = 10;
t.y = 20;
cout << t.x << " " << t.y << endl;
}
int main() {
Bus b;
b.print();
test();
}
Lecture 22 : OOPS 3
//============>>>>>>>>>> Lecture 22 : OOPS 3 NOTES
=>Lecture 22 : OOPS 3 Exception Handling Notes Lecture 22 : OOPS 3 Exception Handling Notes.pdf
=>Lecture 22 : OOPS 3 Notes Lecture 22 : OOPS 3 Notes.pdf
Abstraction & Encapsulation
Inheritance : Introduction
Inheritance : Syntax
Inheritance : Order of Constructor/Destructor cal
Inheritance : Types
Hybrid Inheritance
// jekhane age print pabe oitai print korbe. own class a print function thakle r parent er print function call korbe nah. jekhanei age print function pabe oi khanei age print kore dibe. r uporer print a jabe nah.
Polymorphism : Compile time
Polymorphism : Run time
Virtual functions & Abstract classes
Friend Functions & Classes