0% found this document useful (0 votes)
35 views

Inheritance: 1 Member Type in Base Class Is Public and Type of Derivation Public

The document discusses different cases of inheritance where the member type in the base class (public, private, protected) is varied and the type of derivation (public, private, protected) is also varied. It presents the output or compilation error for accessing members of the base class from the derived class in each case. The key findings are that public members can be accessed directly or through functions for public derivation, private members cannot be accessed at all, and protected members can be accessed through derived class functions for public and protected derivation.

Uploaded by

chaitanya gavit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Inheritance: 1 Member Type in Base Class Is Public and Type of Derivation Public

The document discusses different cases of inheritance where the member type in the base class (public, private, protected) is varied and the type of derivation (public, private, protected) is also varied. It presents the output or compilation error for accessing members of the base class from the derived class in each case. The key findings are that public members can be accessed directly or through functions for public derivation, private members cannot be accessed at all, and protected members can be accessed through derived class functions for public and protected derivation.

Uploaded by

chaitanya gavit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Inheritance

August 12, 2015

1
1.1

Member type in base class is public and type of derivation public


Case 1

Where public variable is accessed directly in the derived class and assigned value.
class base
{
int hpr;
public:
int hpu;
public: void area(int value)
{ hpr=value;
cout<<hpr;
}
};
class derived: public base
{ int hdpr;
public:
int hdpu;
void darea(int value)
{ hpu=value;
cout<<hpu;
}
};
int main()
{ derived d;
d.hpu=10;
cout<<d.hpu;
return 0;
}
Output 10

1.2

Case 2:

Where public variable is accessed and intialized by derived class through member function of base class.
class base
1

{
int hpr;
public:
int hpu;
public: void area(int value)
{ hpu=value;
cout<<hpu;
}
};
class derived: public base
{ int hdpr;
public:
int hdpu;
void darea(int value)
{ hpu=value;
cout<<hpu;
}
};
int main()
{ derived d;
d.area(10);
return 0;
}
Output 10

1.3

Case 3:

Where public variable is accessed and intialized by derived class through member function of derived class.
class base
{
int hpr;
public:
int hpu;
public: void area(int value)
{ hpu=value;
cout<<hpu;
}
};
class derived: public base
{ int hdpr;
public:
int hdpu;
void darea(int value)
{ hpu=value;
2

cout<<hpu;
}
};
int main()
{ derived d;
d.darea(10);
return 0;
}
Output 10

Member type in base class is private and type of derivation public

2.1

Case 1

Where private variable is accessed directly in the derived class and assigned value.
class base
{
int hpr;
public:
int hpu;
public: void area(int value)
{ hpr=value;
cout<<hpr;
}
};
class derived: public base
{ int hdpr;
public:
int hdpu;
void darea(int value)
{ hpr=value;
cout<<hpr;
}
};
int main()
{ derived d;
d.hpr=10;
return 0;
}
Compilation error: hpr is private.

2.2

Case 2:

Where private variable is accessed and intialized by derived class through member function of base class.
3

class base
{
int hpr;
public:
int hpu;
public: void area(int value)
{ hpr=value;
cout<<hpr;
}
};
class derived: public base
{ int hdpr;
public:
int hdpu;
void darea(int value)
{ hpr=value;
cout<<hpr;
}
};
int main()
{ derived d;
d.area(10);
return 0;
}
output: 10

2.3

Case 3:

Where private variable is accessed and intialized by derived class through member function of derived class.
class base
{
int hpr;
public:
int hpu;
public: void area(int value)
{ hpr=value;
cout<<hpr;
}
};
class derived: public base
{ int hdpr;
public:
int hdpu;
void darea(int value)
{ hpr=value;
4

cout<<hpr;
}
};
int main()
{ derived d;
d.darea(10);
return 0;
}
Compilation error:hpr is private

Member type in base class is protected and type of derivation public

3.1

Case 1

Where protected variable is accessed directly in the derived class and assigned value. class base
{
protected: int hpr;
public:
int hpu;
public: void area(int value)
{ hpr=value;
cout<<hpr;
}
};
class derived: public base
{ int hdpr;
public:
int hdpu;
void darea(int value)
{ hpu=value;
cout<<hpu;
}
};
int main()
{ derived d;
d.hpr=10;
cout<<d.hpr;
return 0;
}
Compilation error: hpr is protected

3.2

Case 2:

Where protected variable is accessed and intialized by derived class through member function of base class.

class base
{
protected: int hpr;
public:
int hpu;
public: void area(int value)
{ hpr=value;
cout<<hpr;
}
};
class derived: public base
{ int hdpr;
public:
int hdpu;
void darea(int value)
{ hpr=value;
cout<<hpr;
}
};
int main()
{ derived d;
d.area(10);
return 0;
}
Output: 10

3.3

Case 3:

Where protected variable is accessed and intialized by derived class through member function of derived
class.
class base
{
protected: int hpr;
public:
int hpu;
public: void area(int value)
{ hpr=value;
cout<<hpr;
}
};
class derived: public base
{ int hdpr;
public:
int hdpu;
void darea(int value)
6

{ hpr=value;
cout<<hpr;
}
};
int main()
{ derived d;
d.darea(10);
return 0;
}
Output: 10

Member type in base class is public and type of derivation private

4.1

Case 1

Where public variable is accessed directly in the derived class and assigned value.
class base
{
int hpr;
public:
int hpu;
public: void area(int value)
{ hpu=value;
cout<<hpu;
}
};
class derived: private base
{ int hdpr;
public:
int hdpu;
void darea(int value)
{ hpu=value;
cout<<hpu;
}
};
int main()
{ derived d;
d.hpu=10;
cout<<d.hpu;
return 0;
}
Compilation error:hpu is not accessible

4.2

Case 2:

Where public variable is accessed and intialized by derived class through member function of base class.
class base
{
int hpr;
public:
int hpu;
public: void area(int value)
{ hpr=value;
cout<<hpr;
}
};
class derived: private base
{ int hdpr;
public:
int hdpu;
void darea(int value)
{ hpu=value;
cout<<hpu;
}
};
int main()
{ derived d;
d.area(10);
return 0;
}
Compilation error: Not accessible

4.3

Case 3:

Where public variable is accessed and intialized by derived class through member function of derived class.
class base
{
int hpr;
public:
int hpu;
public: void area(int value)
{ hpu=value;
cout<<hpu;
}
};
class derived: private base
{ int hdpr;

public:
int hdpu;
void darea(int value)
{ hpu=value;
cout<<hpu;
}
};
int main()
{ derived d;
d.darea(10);
return 0;
}
Output:10

4.4

Case 4:

Protected variable will be only accessed and intialized by derived class through member function of derived
class.
class base
{
protected:
int hpr;
public:
int hpu;
public: void area(int value)
{ hpr=value;
cout<<hpr;
}
};
class derived: private base
{ int hdpr;
public:
int hdpu;
void darea(int value)
{ hpr=value;
cout<<hpr;
}
};
int main()
{ derived d;
d.darea(10);
return 0;
}
Output:10

Private variable will not be accessed.

5
5.1

Member type in base class public and type of derivation protected


Case 1

Where public variable is accessed directly in the derived class.


Compilation error:inaccessible.

5.2

Case 2

Where public variable is accessed directly in the derived class and assigned value through member function
of base class.
Compilation error:inaccessible.

5.3

Case 3

Where public variable is accessed directly in the derived class and assigned value through member function
of derived class.
Output:10

Member type in base class private and type of derivation protected

Only be accessed and intialized through member function of the derive class.
Output:10

Member type in base class protected and type of derivation protected

10

You might also like