C + + inheritance is almost the same as in Java, here's a simple example:
Main.cpp:
#include <iostream> #include "Box.h" #include "Carton.h" using std::cout;using std::endl;int Main () {Box Mybox ( 40.0,30.0,20.0); Carton Mycarton; Carton Candycarton ("Thin cardboard") cout << endl<< "Mybox occupies" << sizeof mybox << "bytes" &L T;<endl;cout << "Mycarton occupies" << sizeof Mycarton << "bytes" <<endl;cout << "Candyc Arton occupies "<<sizeof candycarton <<" bytes "<<endl;return 0;}
Box.h:
#ifndef box_h#define box_hclass box{public:box (double lv=1.0,double wv=1.0,double hv=1.0);p rivate:double length; Double width;double height;}; #endif
Carton.h:
#ifndef carton_h#define carton_h#include "Box.h" Class Carton:public Box{public:carton (const char* pstr= "Cardboard"); ~ Carton ();p rivate:char* pmaterial;}; #endif
Box.cpp:
#include "Box.h" Box::box (double lv,double wv,double HV): Length (LV), Width (WV), height (HV) {}
Carton.cpp:
#include "Carton.h" #include <cstring> #include <iostream>carton::carton (const char* pStr) {pmaterial=new Char[strlen (PSTR) +1]; strcpy (PMATERIAL,PSTR);} Carton::~carton () {delete[] pmaterial;}