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

#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<<" ";
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值