Sams Teach Yourself C in 21 Days

(singke) #1
Working with C++ Classes and Objects 689

BD3


Accessing Classes in Classes ........................................................................


You access a class within a class in the same way you would access a nested structure.
Remember that you are accessing data members. To access the xvalue of the starting
point in a line object called line1, you use
line1.start.x
You access the yvalue of the ending point in the same line object by using the following:
Line1.end.y

Inheriting in C++ ................................................................................................


Although C++ offers the capability to nest classes within other classes, C++ also offers a
more powerful feature. This is the third feature of an object-oriented language: inheri-
tance.
Inheritanceis the capability to create new classes by building upon existing classes.
Consider an example. There are people in the world. Every person in the world can be
characterized by a number of features. Characteristics that describe every person include


  • a name

  • an age

  • the country lived in

  • other miscellaneous features
    Now consider an employee at a company. Not every person is an employee at a com-
    pany; however, every employee at a company can be considered a person. An employee
    has all the features of a person listed above, plus additional characteristics such as

  • the name of a company where work is performed

  • a salary

  • other miscellaneous features
    Consider a student. A student is not an employee; however, a student is a person. A stu-
    dent, too, has all the characteristics of a person, plus a few additional characteristics such
    as

  • a student ID

  • a school

  • other miscellaneous features


38 448201x-Bonus3 8/13/02 11:19 AM Page 689

Free download pdf