课本的例题.就是编译不出来,很郁闷
//多重继承,从多个基类派生出来的类,多文件形式
#ifndef BASE1_H
#define BASE1_H
class Base1{
int value;
public:
Base1(int x){value=x;}
int getData() const {return value;}
};
#endif
#ifndef BASE2_H
#define BASE2_H
class Base2{
char letter;
public:
Base2(char c) {letter=c;}
char getData() const {return letter;}
};
#endif
#ifndef DERIVED_H
#define DERIVED_H
#include "base1.h"
#include "base2.h"
class Derived : public Base1,public Base2
{
double real;
public:
Derived(int,char,double);
double getReal() const;
void Output();
};
#endif
//derived.cpp document
#include<iostream>
#include "derived.h"
using namespace std;
Derived::Derived(int i,char c,double f):Base1(i),Base2(c),real(f){ }
double Derived::getReal() const {return real;}
void Derived::Output()
{
cout<<"Integer:"<<Base1::gerData()<<Base2::getData()
<<"\nCharacter:"<<Base2::getData()
<<"\nReal unmber:"<<real<<endl;
}
//Example 10_7.cpp document reborn class
#include<iostream>
#include"base1.h"
#include"base2.h"
#include"derived.h"
using namespace std
int main()
{
Base1 b1(10),*base1Ptr=0;
Base2 b2('z'), *base2ptr=0;
Derived(7,'A',3.5);
cout<<"
Object b1 contains integer"<<b1.getData()
<<"\n
object b2 contains character"<<b2.getData()
<<"\n
Object d contains:\n";
d.Output;
cout<<"Data members of Derived can be"<<"accessd individually:"
<<"\nInter:"<<d.Base1::getData()<<"\ncharacter:"<<d.Base2::getData()
>>"\nreal numbers:"<<d.getReal()<<endl;
cout<<"Derived can be treated as an"<<"abject of either base class:\n;
base1Ptr=&d;
cout<<"base1Ptr->getData()"<<base1Ptr->getData()<<'\n';
base2Ptr=&d;
cout<<"base2Ptr->getData()"<<base2Ptr->getData()<<endl;
return 0;
}