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?.

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is oops concept with example?

574


What is for loop and its syntax?

596


How to call a non virtual function in the derived class by using base class pointer

5253


What is persistence in oop?

669


What is oops in simple words?

577






What are the 3 pillars of oop?

611


why reinterpret cast is considered dangerous?

1898


write a programe to calculate the simple intrest and compund intrest using by function overlading

1666


What is polymorphism programming?

601


i=20;k=0; for(j=1;k-i;k+=j<10?4:3) { cout<

1413


Why do we use polymorphism in oops?

577


Question: Implement a base class Appointment and derived classes Onetime, Daily, Weekly, and Monthly. An appointment has a description (for example, “see the dentist”) and a date and time. Write a virtual function occurs_on(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then fill a vector of Appointment* with a mixture of appointments. Have the user enter a date and print out all appointments that happen on that date.

628


What is oops and its features?

584


What is the significance of classes in oop?

587


What is a null tree?

627