120 likes | 131 Views
Q1. WAP TO DEMONSTRATE HOW AMBIGUITY CAN BE AVOIDED IN SINGLE INHERITANCE USING SCOPE RESOLUTION OPERATOR ? ANS- class m { public: Void display(void) { cout<<”class m<br>”; } }; Class n
E N D
Q1. WAP TO DEMONSTRATE HOW AMBIGUITY CAN BE AVOIDED IN SINGLE INHERITANCE USING SCOPE RESOLUTION OPERATOR ? ANS- class m { public: Void display(void) { cout<<”class m\n”; } }; Class n { public: Void display(void) { cout<<”class n \n”; } }; Class p: public m,public n { public: Void display (void) { m:: display();
}; Int main() { P p1; P1.display() } We may face a problem in using the multiple inheritance when a function with the same inheritance appears in more than one base class consider the classes m and n above . Which display() function if used by derived class when we inherit this two classes. We can solve this problem by defining a name instance within the derived class using the class resolution operator with a function as shown above. Q2. Wap to demonstrate how the private data members cannot be accessed by the public member function of the derived class even though the derived class has been inherited publically. Ans- # include <iostream.h> Class b { int a;
Public: Int b; Void get_ ab(); Int get_a(void); Void show_a(void); }; Class d: public b { int c ; Public: Void mul(void); Void display(void); }; Void b:: get_ab(void) {a=5; b=10;} Int b:: get_a() { return a; } Void b:: show_a() { cout<<”a=”<<a<<”\n”; }void d::mul()
{ c=b*get_a(); } Void d:: display() { cout<<”a=”<<get_a()<<”\n”; Cout<<”b=”<<b<<”\n”; Cout<<”c=”<<c<<”\n”; } Int main() { d d1; d1.get_ab(); d1.mul(); d1.show_a(); d1.display(); d1.b=20;d1.mul();d1.display(); Return 0; } The class is a public derivation of the base class ’d’ therefore ‘d’ inherits all the public members of b and retain their visibility thus the public member of base class b is also a public member of the derived class d The private members of b cannot be inherited by d Q3. What is meant by inheritance in oop paradigm how is direct and indirect base class declared in cpp?
Ans. The mechanism of deriving a new class from an old one is called inheritance the old class is referred to as base class and the new one is called the derived class or sub class the derived class inherits some or all of the trades from the base class Syntax Class derived class-name : visibility mode base class name { ……….. ……………. …………. }; Q4. Explain the relationship among the following terms Super class. Sub class. Base class. Derived class. Ans: - Class a { -------- -------- };
Class b: visibility mode a {----------- ----------- }; Here class a is known as the base class the derived classes will Inherit some or all properties from it, class a is the derived class Or the sub class and will inherit the base class either publicaly or privately. Q5Develop a OOP in CPP to read the following information from the keyboard in which the base class consists of: --ename,--ecode,--designation,--years of experience,--age. Ans:- #include<iostream.h> #include<conio.h> class base
void display() { cout<<"the name of the employee is"<<emp_name; cout<<"the code is"<<code; cout<<"the designation of the employee is"<<designation; cout<<"the year of experience are"<<yoe; cout<<"the age of employee"<<age; }
}; Void main() { Clrscr(); Base b; b.getdata(); b.display(); getch(); } Q6Create a following class network: PERSON(NAME,CODE)ACCOUNT(PAY) PERSON(NAME,CODE)ADMIN(EXPERIENCE) ACCOUNT(PAY),ADMIN(EXPERIENCE)PERSON(NAME,CODE,EXPERIENCE,PAY) (Here left side depicts the base class and right side depicts the derived class name)
ANS 6:- #include<iostream.h> #include<conio.h> #include<stdio.h> Class person { Int code; Char name[15]; Private: Void get() { Cout<<”enter name :”; Gets(nam); Cout<<”enter code”; Cin>>code; } Void display() { Cout<<”name:”; Puts(name); Cout<<”code:”<<code; } }; Class account: virtual public person { Float pay; Public: Void getpay() { Cout<<”enter pay :”; Cin>>pay; } Void putpay() { Cout<<”pay:”<<pay;}
}; Class admin:public virtual person {int exp; Public: Void getexp() { Cout<<”enter experience”; Cin>>exp; } Void putexp() { Cout<<”experience:”<<exp; } }; Void main() {clrscr(); Admin a; Account b;
a.get(); a.getpay(); b.getexp(); a.put(); a.putpay(); b.putexp(); getch(); } Q7. Debug the following. State reasons? Ansline 42:-E must be previously define class or struct. Line 69:-E::X1 NOT accessible. LINE 80:-E::X1,not accessible. LINE 86:-MULTIPLE declaration for E::main Line 53 :-type name expected