Unit - 3 OOP 1
Unit - 3 OOP 1
Unit-3 (CIC-211)
Part-1
Prepared By: Dr. Ankita Sharma
Assistant Professor
Department of Computer Science
GTBIT, GGSIPU
Agenda
1. Inheritance
2. Inheritance Methods
3. Class Hierarchy
4. Derivation-Public, Private & Protected
5. Aggregation
6. Inheritance Constructors
Where 'A' is the base class, and 'B' is the derived class.
class Base2 {
// Base2 members
};
class Derived : public Base1, public Base2 {
// Derived class members
};
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class PublicDerived : public Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
};
int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class PublicDerived : public Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
};
int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}