C# Practical : Inherit from Existing Class to Create New Class

Leave a Comment

Create a class named 'B' that inherits from class 'A'. Create three private integer values in the class. Add code like the following:
class B : A
{
   private int m_x;
   private int m_y;
   private int m_z;
}

Just like in C++, the colon operator indicates inheritance from a base class. C# classes can hold data members and member functions, just as in C++.

Create an instance of B. Call its member functions, and attempt to access its private data members. Within the Main function, type code like the following:
B myB = new B();
B.Static_Member();
myB.Instance_Member();
m_x = 1;
m_y = 2;
m_z = 3;

You will be able to call the member functions, but the private data members are not accessible. For that you need to define properties. A property looks like a data member, but it has a public "get" and/or "set" member that allows the reading or writing of a value that is controlled by a function call.

0 comments:

Post a Comment