You are on page 1of 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