0% found this document useful (0 votes)
3 views2 pages

Tutorial-11 - Inheritance and Virtual Functions

The document contains a tutorial for a B. Tech. (CSE) course focused on Object Oriented Programming, specifically discussing the output of various C++ programs. Each program demonstrates different aspects of class inheritance, constructors, destructors, and virtual functions. The tutorial includes multiple code snippets that require prediction of their outputs.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Tutorial-11 - Inheritance and Virtual Functions

The document contains a tutorial for a B. Tech. (CSE) course focused on Object Oriented Programming, specifically discussing the output of various C++ programs. Each program demonstrates different aspects of class inheritance, constructors, destructors, and virtual functions. The tutorial includes multiple code snippets that require prediction of their outputs.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Jaypee University of Engineering and Technology

B. Tech. (CSE) - II Semester


Object Oriented Programming (CS102)
Tutorial – 11

Predict the output of following programs.


1. #include <iostream> 2.
using namespace std; #include <iostream>
class A{ using namespace std;
public: class A
A(){ cout<<"A " ;} {public:
~A(){ cout<<“~A " ;} A(){ cout<<"A\n" ;}
}; };
class B : A{ class B
public: {public:
B(){ cout<<"B " ;} B(){ cout<<"B\n" ;}
~B(){ cout<<“~B " ;} };
}; class C :public B,private A
class C :public A{ {public:
public: C(){ cout<<"C\n" ;}
C(){ cout<<"C " ;} };
~C(){ cout<<“~C " ;} class D: public C ,A,B
}; {public:
class D :B,C { D() { cout<<"D";}
public: };
D() { cout<<"D ";} int main() {
~D() { cout<<“~D ";} D d;
}; return 0;
int main() { }
D d; A a; C c;
}
3. #include<iostream> 4.
using namespace std; class Base{
class A { public:
public: virtual void show()=0;
virtual void fun() };
{ cout<<"\n A::fun() called “; } class Derv1:public Base{
}; public:
class B: public A { void show(){cout<<“Derv1\n”;}
public: };
void fun() class Derv2:public Base{
{ cout<<"\n B::fun() called "; } public:
}; void show(){cout<<”Derv2\n”;}
class C: public B { };
public: int main()
void fun() {
{ cout<<"\n C::fun() called "; } base* arr[2];
}; derv1 dv1;
int main() derv2 dv2;
{ arr[0]=&dv1;
C c; // An object of class C arr[1]=&dv2;
B *b = &c; arr[0]->show();
b->fun(); arr[1]->show();
return 0; return 0;
} }
5. #include <iostream> 6.
using namespace std; #include <iostream>
class A { using namespace std;
public: class Base {
virtual ~A() { cout << "A "; } public:
}; Base() { cout << "Base Constructor\n"; }
class B : public A { virtual ~Base() { cout << "Base Destructor\n"; }
public: };
~B() { cout << "B "; } class Derived : public Base {
}; public:
class C : public B { Derived() { cout << "Derived Constructor\n"; }
public: ~Derived() { cout << "Derived Destructor\n"; }
~C() { cout << "C "; } };
}; int main() {
int main() { Base* ptr = new Derived();
A* obj = new C(); delete ptr;
delete obj; }
}

You might also like