Base class has two public data members. How can i derive a
new class with one datamember as public and another data
member as private?.

Answers were Sorted based on User's Feedback



Base class has two public data members. How can i derive a new class with one datamember as public..

Answer / deepak sharma

class base
{
public:

base(int d1 = 5, int d2 = 6) : data1(d1), data2(d2)
{ }
int data1, data2;
};

class der:public base
{
private:
using base::data1; //Making data1 of class base
private, explicitliy,
//you can make member functions private or
protected this way
};

int main(int argc, char* argv[])
{
der obj1;
cout<<obj1.data1<<endl; //Error : 'data1' : cannot
access private member declared in class 'der'
cout<<obj1.data2<<endl; //Works fine
return 0;
}

Is This Answer Correct ?    9 Yes 0 No

Base class has two public data members. How can i derive a new class with one datamember as public..

Answer / iyappan

just have a look on the given program. Then you can get
idea about it.
class base
{
public:
virtual void fun1(){printf("I am base class public
fun1");}
virtual void fun2(){printf("I am base class public
fun2");}

};
class derived:public base
{
void fun1(){printf("I am derived class private
fun1");}
public:
void fun2(){printf("I am derived class public
fun2");}
};

int main()
{
derived obj;
obj.fun1();//it will show compile time error.
because this is private member of derived class
obj.fun2();//it will work fine.
base *ptr = &obj;
ptr->fun1();//it will work fine.
ptr->fun2();//it will work fine.
}

Is This Answer Correct ?    2 Yes 3 No

Post New Answer

More OOPS Interview Questions

What is extreme programming?

2 Answers  


Write a program to sort the number with different sorts in one program ??

0 Answers   NIIT,


What is methods in oop?

0 Answers  


What is destructor oops?

0 Answers  


Why many objects can working together? How objects working togetherM I want to see example code.

2 Answers  






hi, this is raju,iam studying b.tech 2nd year,iam want know about group1 and group2 details, and we can studying without going to any instutions? please help me.

0 Answers  


what is the application of oops?

8 Answers   IBM,


What is pointer in oop?

0 Answers  


In OverLoading concept,Why they are not consider return value and why they are consider only parameters in method? For ex: public int Add(int a,int b){...} public String Add(int a,int b){...}

1 Answers  


What is encapsulation selenium?

0 Answers  


Why we are use # in begning of programme of c++.

2 Answers   Syntel,


WAP to find the ambiguities in Multiple Inheritance? How are they resolved.(Virtual Functions)

1 Answers  


Categories