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.
No comments:
Post a Comment