<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 31 Multiple Inheritance_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 31: Multiple Inheritance
 More of this Feature
• Introduction
• Analysis of Car Class
• JetCar, First Attempt
• Percolating Up
• Casting Down
• Casting Down, Continued
• Turning on RTTI - VC++.net
• Turning on RTTI - VC++ 6.0
• Multiple Inheritance
• Output and Analysis
• Output and Analysis, Continued
 
 Related Resources
• Inheritance
• Polymorphism
• Virtual Inheritance
• Advanced C++ Tutorials
• Simple Solutions in C++
• C++ Programming Tips
• C++ Tutorial, See all lessons
 

by John Kopp

Introduction
Welcome to About.com's free tutorial on C++ programming. This lesson covers multiple inheritance. In C++, a class may have multiple base classes. That is, it may inherit the members and methods of multiple base classes. As an example of this, I will walk you through some recent design I have done for my top secret Jet-Car project. Over the last year, tired of both endless construction on the roads and increased time to pass through tightened airport security, I have embarked on producing the only reasonable solution to my commuting nightmares, a homemade Jet-Car.

As I started on my Jet-Car design, I began with two existing classes I developed for earlier lessons, the Vehicle and Car classes.

#include <iostream>
using namespace std;

class Vehicle {
public:
    Vehicle() {cout << "Vehicle Constructor" << endl;}
    virtual ~Vehicle() {cout << "Vehicle Destructor" << endl;}

    virtual void accelerate() const {cout << "Vehicle Accelerating" << endl;}

    void setAcceleration(double a) {acceleration = a;}
    double getAcceleration() const {return acceleration;}

private:
double acceleration;
};

class Car: public Vehicle {
public:
    Car() {cout << "Car Constructor" << endl;}
    virtual ~Car() {cout << "Car Destructor" << endl;}

    virtual void accelerate() const {cout << "Car Accelerating" << endl;}
    void drive() const {cout << "Car Driving" << endl;}

private:
    // Car inherits acceleration accessors, member
};

int main() {

    Car myCar;

    myCar.setAcceleration(9.81);
        //One "G"

    cout << "Accelerating at " << myCar.getAcceleration() << " m/(s*s)";
    cout << endl;

    myCar.Accelerate();
    myCar.Drive();

}

Next page > Analysis of Car Class > Page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14


Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.