#include<iostream> using namespace std; //实现多继承场景 class Interface { public: virtual int add(int a,int b)=0; virtual void print()=0; private: int i1; protected: }; class Interface2 { public: virtual int multi(int a,int b)=0; virtual void print()=0; private: int i2; protected: }; class Child:public Interface,public Interface2 { public: void print() { cout<<"Child 的 打印 "; } int add(int a,int b) { return a+b; } int multi(int a,int b) { return a*b; } private: int c; protected: }; int main() { Child c; c.print(); cout<<" add"; cout<<c.add(2,3); cout<<endl; cout<<" multi "; cout<<c.multi(3,4); cout<<endl; Interface *i1=&c; Interface2 *i2 =&c; cout<<i1->add(2,4); cout<<endl; cout<<i2->multi(7,8); cout<<endl; i1->print(); i2->print(); system("pause"); return 0; }
#include<iostream> using namespace std; //编写一个C++程序, 计算程序员( programmer )工资 // 1 要求能计算出初级程序员( junior_programmer ) 中级程序员 ( mid_programmer )高级程序员( adv_programmer)的工资 // 2 要求利用抽象类统一界面,方便程序的扩展, 比如:新增, 计算 架构师 (architect ) 的工资 // class saraly { public: virtual void salary()=0; }; class junior_programmer:public saraly { public: junior_programmer(char *name,char *kind,int sal) { this->kind=kind; this->name=name; this->sal=sal; } virtual void salary() { cout<<"the salary of the junior_programmer is "<<sal<<endl; } protected: private: char *name ; char *kind; int sal; }; class mid_programmer:public saraly { public: mid_programmer(char *name,char *kind,int sal) { this->kind=kind; this->name=name; this->sal=sal; } virtual void salary() { cout<<"the salary of the mid_programmer is "<<sal<<endl; } protected: private: char *name ; char *kind; int sal; }; class adv_programmer:public saraly { public: adv_programmer(char *name,char *kind,int sal) { this->kind=kind; this->name=name; this->sal=sal; } virtual void salary() { cout<<"the salary of the adv_programmer is "<<sal<<endl; } protected: private: char *name ; char *kind; int sal; }; void Getsalary(saraly *base) { base->salary(); } int main() { junior_programmer jp("Alice ","junior ",400); mid_programmer mp("Alenx","mid",8000); adv_programmer ap("Juster", "adv",1500); Getsalary(&jp); Getsalary(&mp); Getsalary(&ap); system("pause"); return 0; }