您的位置:首页 > 编程语言 > C语言/C++

(承接上一篇)类方法——C++实现方式

2015-07-25 10:26 483 查看
#include<iostream>

#include<string>

using namespace std;

class People

{

protected:
string name,sex,tel;
int age;

public:

    People(string name,string sex,int age,string tel);
People(){}
People(People &a);
void Show();

};

People::People(string name,string sex,int age,string tel)

{
this->name=name;
this->sex=sex;
this->age=age;
this->tel=tel;

}

void People::Show()

{
cout<<"name: "<<name<<" sex: "<<sex<<" age: "<<age<<" tel: "<<tel<<endl;

}

People::People(People &a)

{
name=a.name;
age=a.age;
sex=a.sex;
tel=a.tel;

}

class Teacher:virtual public People

{

protected:
string tepost;

public:
Teacher(){}
Teacher(People &a,string tepost);
Teacher(Teacher &a);
void Show();

};

Teacher::Teacher(People &a,string tepost):People(a)

{
this->tepost=tepost;

}

Teacher::Teacher(Teacher &a):People(a)

{
tepost=a.tepost;

}

void Teacher::Show()

{
People::Show();
cout<<"the title of a technical post: "<<tepost<<endl;

}

class cadre:virtual public People

{

protected:
string post;

public:
cadre(){}
cadre(People &a,string post);
cadre(cadre &a);
void Show();

};

cadre::cadre(People &a,string post):People(a)                                                                                  

{
this->post=post;

}

cadre::cadre(cadre &a):People(a)

{
post=a.post;

}

void cadre::Show()

{
People::Show();
cout<<" post: "<<post<<endl;

}

class Double:public Teacher,public cadre

{

private:
int wage;

public:
Double(){};
Double(Teacher &a, cadre &b,int wage);
Double(Double &a);
void Show();

};

Double::Double(Teacher &a, cadre &b,int wage):People(a),Teacher(a),cadre(b)

{
this->wage=wage;

}

Double::Double(Double &a):Teacher(a),cadre(a),People(a)

{
wage=a.wage;

}

void Double::Show()

{

People::Show();
cout<<" post: "<<post<<"  the title of a technical post:  "<<tepost<<" wage: "<<wage<<endl;

}

void main()

{
People a("小李","男",11,"15671641760");

// a.Show();

// cout<<endl;
Teacher b(a,"中级");
b.Show();
cout<<endl;
cadre c(a,"hj");

// c.Show();

// cout<<endl;
Double d(b,c,20000);
d.Show();
}

#include<iostream>

using namespace std;

class A

{

public:

    A(int i){a=i;cout<<"con ced "<<a<<endl;}

    A(){a=0;cout<<"D c ced "<<a<<endl;}

    ~A(){cout<<"D Ced "<<a<<endl;}

private:

    int a;

};

int main(){

    A a[4];

    int n=1;int i;

    for(i=0;i<4;i++)

        a[i]=A(++n);

    return 0;

}

#include<iostream>

using namespace std;

class A

{

public:

    A(int i):a(i){cout<<"A:c ced\n";}

    ~A(){cout<<"A D ced\n";}

    void Print(){cout<<a<<endl;}

    int Geta(){return a;}

private:

    int a;

};

class B:public A

{

public:

    B(int i=0,int j=0):A(i),a(j),b(i+j){cout<<"B:c ced\n";}

    ~B(){cout<<"B D ced\n";}

    void Print(){A::Print(); cout<<b<<','<<a.Geta()<<endl;}

private:

    int b;

    A a;

};

int main()

{

    B b1(8),b2(12,15);

    b1.Print();

    b2.Print();

    return 0;

}

#include<iostream>

using namespace std;

class A

{

public:

    A(){cout<<"In A c"<<endl;}

    virtual ~A(){cout<<"In A d"<<endl;}

    virtual void f1(){cout<<"In A f1"<<endl;}

    void f2(){f1();}

};

class B:public A

{

public:

    B(){f1();cout<<"In B c"<<endl;}

    ~B(){cout<<"In B d"<<endl;}

};

class C:public B

{

public:

    C(){cout<<"In C c"<<endl;}

    ~C(){cout<<"In C d"<<endl;}

    void f1(){cout<<"In C f1"<<endl;}

};

int main()

{

    A *pa=new C;

    pa->f2();

    delete pa;

    return 0;

}

#include<iostream>

using namespace std;

class A

{

public:

    ~A(){cout<<"A";}

};

char fun()

{

    A a;

    throw('B');

    return '0';

}

int main()

{

    try

    {

        cout<<fun()<<" ";

    }

    catch(char b)

    {

        cout<<b<<" ";

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: