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
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 |
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 |
What are objects in oop?
What do you mean by multiple inheritance and multilevel inheritance? Differentiate between them.
what is code for call by value and call by reference?
What is the difference between a mixin and inheritance?
When a private constructer is being inherited from one class to another class and when the object is instantiated is the space reserved for this private variable in the memory??
different types of castings
What is the real time example of encapsulation?
1234554321 1234 4321 123 321 12 21 1 1 12 21 123 321 1234 4321 1234554321
me get an assignent n its question is this 1.creat a set as in math i.ea={1,2} 2.insert element in it3. delete element don,t repeat any element 4.union 5. intersection of two sets plz help me i always pray for u n send me at ayeshawzd@hotmail.com f u have c++ how to program 5th addition then it is the 10.9 question in 10th chapter exercise
what is cast operator?
design class for linked list and include constructor,destructor,insert option. node of form struct node { int data; struct node &ptr; }
When is a memory allocated to a class?