(承接上一篇)类方法——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<<" ";
}
}
#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<<" ";
}
}
相关文章推荐
- 杭电2149
- memcpy的实现
- C语言学习笔记
- 杭电2139
- C/C++入门基础 之类方法 入门篇
- C++基础 虚函数、重载、覆盖、隐藏
- PAT (Basic Level) Practise (中文)1034. 有理数四则运算(20) C语言
- C 语言和C++语言的 struct对比
- OC语言字典
- C语言中.h和.c文件解析
- c++静态成员
- 变量的使用注意
- 变量的内存分析
- C语言数据类型总结
- java 使用JNI调用C语言
- 黑马程序员-ios学习笔记 c语言 结构体
- IOS开发学习记录第3天之C语言学习
- 学艺不精而羞愧--论C++模板类的使用
- C++ outline and interview questions (1): Variable
- 《C++编程思想》(第二版)第3章 C++中的C(笔记、习题及答案)(一)