Practical CPP (9-12)
Practical CPP (9-12)
CODE:
#include <iostream>
using namespace std;
class Hello {
public:
void displayBase() {
cout << "Hello from Base Class" << endl;
}
};
int main() {
Hello2 obj;
obj.displayBase();
obj.displayDerived();
return 0;
}
OUTPUT:
PRACTICAL -11
(Q: Write a C++ program that illustrate multiple inheritance .)
CODE:
#include <iostream>
using namespace std;
class Clg {
protected:
string name;
public:
void setCollegeName(string x) {
name = x;
}
void displayCollege() {
cout << "College Name: " << name << endl;
}
};
class Div {
protected:
string div_;
public:
void setDivisionName(string y) {
div_ = y;
}
void displayDivision() {
cout << "Division: " << div_ << endl;
}
};
OUTPUT:
PRACTICAL -12
(Q: Write a C++ program that illustrate multi-level inheritance . )
CODE:
#include <iostream>
using namespace std;
class Person {
protected:
string name;
public:
void setName(string n) {
name = n;
}
void display() {
cout << "Name: " << name << endl;
}
};
CODE:
#include <iostream>
using namespace std;
class Person {
protected:
string name;
public:
void setName(string n) {
name = n;
}
void display() {
cout << "Person: " << name << endl;
}
};
a.setName("Madhur");
b.setName("kartikay");
a.display();
a.say();
b.display();
b.say2();
return 0;
}
OUTPUT:
PRACTICAL -14
(Q: Write a C++ program to illustrate the order of the exhibition of the
constructor and destructor .)
CODE:
#include <iostream>
using namespace std;
class Base {
public:
Base() {
cout << "Base Constructor" << endl;
}
~Base() {
cout << "Base Destructor" << endl;
}
};
~Derived() {
cout << "Derived Destructor" << endl;
}
};
int main() {
Derived obj;
cout << "Madhur Bansal" << endl;
return 0;
}
OUTPUT:
PRACTICAL -15
(Q: Write a program to implement friend function by taking some real-life
example .)
CODE:
#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
void displayPersonDetails(Person p) {
cout << "Name: " << p.name << endl;
cout << "Age: " << p.age << " years" << endl;
}
int main() {
Person person1("Madhur bansal ,19);
displayPersonDetails(person1);
return 0;
}
OUTPUT: