Q1) Output of the
program
#include
"iostream.h"
#include
"conio.h"
class abc
{
private:int a;
public :void
getdata()
{
a=100;
};
void putdata()
{
cout<<a;
};
};
main()
{
abc p;
abc *q=&p;
q->getdata();
q->putdata();
getch();
}
Ans:-100
Discussion:-Here q
is a pointer of abc class type .And it takes a address of abc class object.
Q2)Output of the
program
#include
"iostream.h"
#include
"conio.h"
class test
{
private :int a;
int b;
};
main()
{
test t;
test *p;
p=&t;
p->a=100;
p->b=200;
getch();
}
Ans:-Compile time
error.
Discussion:-Here
a and b are private data member so, It is not possible to access
the data member using pointer directly.
Q3) Output of the
program.
#include
"iostream.h"
#include
"conio.h"
class test
{
private :int a;
int b;
test *p;
public :void getdata()
{
test t;
p=&t;
p->a=100;
p->b=200;
};
void putdata()
{
cout<<p->a;
cout<<p->b;
}
};
main()
{
test t;
t.getdata();
t.putdata();
getch();
}
Ans:-100 200
Discussion:-Here p
is a pointer of test class ,and it is declared in private part of the class
definition.
As p is a pointer it is capable of hold the adress of a class object. And we
can access class member using ->(Arrow) operator using pointer.
Q4)Output of the
program.
#include
"iostream.h"
#include
"conio.h"
class test
{
private :int a;
int b;
public :void getdata()
{
a=10;
b=10;
};
void putdata()
{
cout<<a;
cout<<b;
}
};
main()
{
test t;
test *ptr=new
test;
//Pointer declaration using new operator.
ptr->getdata();
ptr->putdata();
getch();
}
Ans :- 10 10
Discussion:-Here
pointer is declared using new operator.
Q5) Output of the
program.
#include
"iostream.h"
#include
"conio.h"
class base
{
public:void display()
{
cout<<"Base class";
};
};
class derive:
public base
{
public:void
display()
{
cout<<"Derive class";
};
};
main()
{
base b;
derive d;
base
*p=&b;
p->display();
p=&d;
p->display();
getch();
}
Ans:-Base class.
Discussion:-Here base
and derive class containing same display() function signature. So
when the derive class display() function will call ,the compiler just
neglect the call and compilat just call to the function according to pointer,
ie in which class the pointer is declared.
Q6)Output of the
program.
#include
"iostream.h"
#include
"conio.h"
class base
{
public:virtual void display()
{
cout<<"Base class";
};
};
class
derive:public base
{
public:void
display()
{
cout<<"Derive class";
};
};
main()
{
base b;
derive d;
base
*p=&b;
p->display();
p=&d;
p->display();
getch();
}
Ans:-Base
class Derived class.
Discussion:-Here in
base class the display() function is declared as virtual function. So the
display() function will call according to initialization of object into
pointer.
Q7) Output of the
program
#include
"iostream.h"
#include
"conio.h"
class base
{
public:void
print()
{
cout<<"Hello";
};
};
main()
{
base b[10];
base
*p=&b[0];
p->print();
getch();
}
Ans:-Hello
Discussion:-Here array
of object has created. And p pointer holding the address of b[0]
object .
Q8)Output of the
program
#include
"iostream.h"
#include
"conio.h"
class test
{
public:void
hello()
{
cout<<"Hello";
};
};
main()
{
test t;
test *ptr;
(*ptr).hello();
//Hello function call.
getch();
}
Ans:-Hello.
Discussion:-Hello
function is invoking using pointer of the test class.
Q9)Output of the
program.
#include
"iostream.h"
#include "conio.h"
class abc
{
public :int a;
void print()
{
cout<<this->a;
};
};
main()
{
abc t;
t.a=100;
t.print();
getch();
}
output:-100
discussion:-This
pointer always point current object member.
Q10)Output of the
program.
#include
"iostream.h"
#include
"conio.h"
class abc
{
public :void
print()
{
cout<<"I am base function";
};
};
class xyz:public abc
{
public :void print()
{
cout<<"I am base function";
};
};
main()
{
xyz *p;
abc a;
p=&a;
getch();
}
ans:-Error
Discussion:-Here the
derive class pointer is trying to assign with the base class object.
Q11)Output of the
program
#include
"iostream.h"
#include
"conio.h"
class abc
{
public
:virtual void print()=0;
};
class xyz:public abc
{
public:void print()
{
cout<<"I am derive function";
};
};
main()
{
xyz t,*p;
p=&t;
p->print();
getch();
}
Output:-I am derive
function
Discussion:-Here in
base class the print function is declared as pure virtual function.
Q12)Output of the
program.
#include
"iostream.h"
#include
"conio.h"
class abc
{
private:int
a,b;
void hello()
{
cout<<"I am hello function";
};
};
main()
{
abc *t=new
abc();
cout<<sizeof(*t);
getch();
}
Output:-4 (in 32 bit
turbo c++ compilar)
Discussion:-Output
is compilar dependent.Here two int data are declared in abc class.So result is
4.
Q13)Output of the
program
#include
"iostream.h"
#include
"conio.h"
main()
{
int *p=new
int[10];
*p=10;
cout<<*p;
getch();
}
Ans:-10
Discussion:-Here new
operator creates 10*2=10 byte memory and initialize the base adress into p.
Q14)Output of the
program
#include "iostream.h"
#include
"conio.h"
class string
{
private:char
*p;
public :char *
hello(char *t)
{
p=t;
return p;
};
};
main()
{
string s;
char
*c=s.hello("Hello world");
cout<<c;
getch();
}
Ans:-Hello world
Discussion:-Here
string pointer is passing as a hello function argument and the hello function
is returning the string pointer again in main function.
#include
"iostream.h"
#include
"conio.h"
class test
{
private:int
a;
int b;
public:void
getdata()
{
a=10;
b=10;
};
void putdata()
{
cout<<a;
cout<<b;
};
void operator-()
{
a=-a;
b=-b;
};
};
main()
{
test t;
t.getdata();
-t;
t.putdata();
getch();
}
Ans:-
-10 -10
Discussion:-Here
–(Ve) operator is overloading. And it changes the sign of a and b.
Q2)Output of the
program
#include
"iostream.h"
#include
"conio.h"
class test
{
private:int
a;
int b;
public:void
getdata()
{
a=10;
b=10;
};
void putdata()
{
cout<<a;
cout<<b;
};
void operator::()
{
a=-a;
b=-b;
};
};
main()
{
test t;
t.getdata();
::t;
t.putdata();
getch();
}
Ans:-Compile time
error.
Discussion:-Here
:: (Scope resolution) operator is trying to overload. But it not possible
to overload scope resolution operator.
Q3)Output of the
program.
#include
"iostream.h"
#include "conio.h"
class test
{
private:int
a;
int b;
public:void
getdata()
{
a=10;
b=10;
};
void putdata()
{
cout<<a;
cout<<b;
};
test operator+(test t)
{
test temp;
temp.a=a+t.a;
temp.b=b+t.b;
return temp;
};
};
main()
{
test t1,t2,t3;
t1.getdata();
t2.getdata();
t3=t1+t2;
t3.putdata();
getch();
}
Ans:-20 20
Discussion:-Here
binary (+) operator is overloaded. And It adding two object t 1 and t2.
Q1)Output of the
program.
#include
"iostream.h"
#include
"conio.h"
class base
{
private:void
print()
{
cout<<"Base class";
}
};
class derive:public
base
{
public:void
call()
{
cout<<"Derive class";
}
}
main()
{
derive d;
d.print();
getch();
}
Ans:-Compile time
error.
Discussion:-Here
derive class is publicly inherited from base class. So the print() function of
base class remain private member in derive class. So the print() function
call is not possible using direct object.
Q2)Output of the
program.
#include
"iostream.h"
#include
"conio.h"
class base
{
private:int a;
public: void
print()
{
a=100;
cout<<a;
}
};
class derive:public
base
{
public:void
call()
{
print();
}
};
main()
{
derive d;
d.call();
getch();
}
Ans-100
Discussion:-Here
derive class is publicly inherited from base class. So derive class
function call() is calling the base class function print().
Q3) Output of the
program.
#include
"iostream.h"
#include
"conio.h"
class base
{
public:
base()
{
cout<<"Base class constructor"<<endl;
}
};
class derive:public
base
{
public:derive()
{
cout<<"Derive class constructor"<<endl;
base();
}
};
main()
{
derive d;
getch();
}
Ans:-Base class
constructor.
Derive class constructor.
Base class constructor.
Discussion:-When the
constructor of derive class is call, then at first the base class constructor
is called then sub class constructor is call.
Q4)Output of the
program
#include
"iostream.h"
#include
"conio.h"
class base
{
public:void
abc()
{
cout<<"Base class"<<endl;
};
};
class derive:private
base
{
public:void
xyz()
{
abc();
};
};
main()
{
derive d;
d.abc();
getch();
}
Ans:-Compile time
error.
Discussion:-Here
derive class is privately inherited from base class. So public
member of base class will be the private member of sub class.
Q5)Output of the
program
#include
"iostream.h"
#include
"conio.h"
class base
{
public:void
abc()
{
cout<<"Base class"<<endl;
};
};
class derive:private
base
{
public:void
xyz()
{
abc();
};
};
class low:public
derive
{
public:void hello()
{
xyz();
}
};
main()
{
low l;
l.hello();
getch();
}
Ans:-Base class.
Discussion:-It is
the example of multi level inheritance. Her e hello() function is calling it’s
superclass xyz() function and xyz() function is calling it’s super class
function abc().And within abc() function ,the message is printing.
Q6)Output of the
program.
#include
"iostream.h"
#include
"conio.h"
class base
{
protected:void abc()
{
cout<<"Base class"<<endl;
};
};
class derive:private
base
{
public:void
xyz()
{
abc();
};
};
main()
{
derive d;
d.xyz();
getch();
}
Ans:-Base class.
Discussion:-Here
derive class is privately inherited from base class. So protected member of
base class will be the private member of derive class.
Q7)Output of the
program.
#include
"iostream.h"
#include
"conio.h"
class base
{
protected:void abc()
{
cout<<"Base class"<<endl;
};
};
class derive:public
base
{
public:void
xyz()
{
abc();
};
};
main()
{
derive d;
d.abc();
getch();
}
Ans:-Compile time
error.
Discussion:-Here
derive class is inherited publicly. So the protected function member abc()
remain protected in derive class. So it is not possible to access the
protect function member using . operator.
Q8) Output of the
program.
#include
"iostream.h"
#include
"conio.h"
class abc
{
private
:void abcfun()
{
cout<<"Super class1"<<endl;
};
public:
void callabc()
{
abcfun();
}
};
class xyz:public abc
{
public:void
xyzfun()
{
cout<<"Super class2";
callabc();
};
};
class pqr:private
abc,public xyz
{
public :void
pqrfun()
{
cout<<"Derive class";
}
};
main()
{
pqr p;
p.pqrfun();
p.xyzfun();
getch();
}
Ans:-Derive class
Super class2 Super class1
Discussion:- It is a
example of hierarchical inheritance. Here xyz class is publicly inherited
from base class abc and class pqr is inherited both from abc as privately
and xyz as publicly.
Q9)Output of the
program.
#include
"iostream.h"
#include
"conio.h"
class abc
{
private:int a;
public:abc()
{
cout<<"Hello";
};
};
class xyz:public abc
{
private:int x;
public:xyz()
{
abc();
};
};
class pqr:public
xyz,public abc
{
public:pqr()
{
xyz();
}
};
main()
{
pqr p;
getch();
}
Ans:-HelloHelloHelloHello
Q10)Output of the
program
#include
"iostream.h"
#include
"conio.h"
class abc
{
public:void print()
{
cout<<"abc";
};
};
class xyz
{
public:void
print()
{
cout<<"xyz";
};
};
class pqr: public
xyz,public abc
{
};
main()
{
pqr p;
p.abc::print();
//Call to print function of abc class
p.xyz::print();
// Call to print function of xyz class
getch();
}
Ans:-abc xyz
Discussion:-Here
pqr class is publicly inherited from both the class abc and xyz.
And is print() function is common both two class ,so we can invoke individual
print() function using scope resolution operator.