目录
一、类和对象的创建和使用
1、设计学生信息类
2、汽车类
3、长方形类
二、构造函数与析构函数:
1、学生信息类
2、对象数组-学生信息表
三、类对象作为函数形参
四、类对象作为输出参数
五、对象作为函数返回值
六、动态内存分配:
1、成绩处理
2、统计学生成绩
七、运算符重载与友元函数:
1、学生信息转换
2、矩阵运算
3、复数运算
八、类的多态性和虚函数:
1、计算图像面积
2、人与复读机
3、复读机的毁灭
九、类的继承与派生:
1、公有继承
2、保护继承
3、私有继承
4、多继承
十、静态成员-模拟共享书店
#include
using namespace std;
class StInfo
{
public:
int SID;
char *Name;
char *Class;
char *Phone;
void SetInfo(int sid,char *name,char* cla,char* phone);
void PrintInfo();
};
void StInfo::SetInfo(int sid,char *name,char* cla,char* phone)
{
SID=sid;
Name=name;
Class=cla;
Phone=phone;
}
void StInfo::PrintInfo()
{
cout<<"学号:"<>SID>>str1>>str2>>str3;
char *Name=(char*)str1.c_str(),* Class=(char*)str2.c_str(),* Phone=(char*)str3.c_str();
StInfo info;
info.SetInfo(SID,Name,Class,Phone);
info.PrintInfo();
return 0;
}
#include
#include
using namespace std;
class Car
{
public:
char door[10];
char light[10];
int speed;
void od()//开门
{
strcpy(door,"ON");
}
void cd()
{
strcpy(door,"OFF");
}
void ol()//开灯
{
strcpy(light,"ON");
}
void cl()
{
strcpy(light,"OFF");
}
void su()
{
speed+=10;
}
void sl()
{
speed-=10;
}
void begin();
};
void Car::begin()
{
strcpy(door,"OFF");
strcpy(light,"OFF");
speed=0;
}
int main()
{
Car bwm;
char cmds[25];
cin>>cmds;
bwm.begin();
int i;
for(i=0;cmds[i]!='\0';i++)
{
switch(cmds[i])
{
case '1':bwm.od();
break;
case '2':bwm.cd();
break;
case '3':bwm.ol();
break;
case '4':bwm.cl();
break;
case '5':bwm.su();
break;
case '6':bwm.sl();
break;
default:break;
}
}
cout<<"车门 "<
#include
using namespace std;
class Rectangle
{
private:
int height;
int width;
public:
int GetArea();
void Set(int h, int w);
};
void Rectangle::Set(int h,int w)
{
height=h;
width=w;
}
int Rectangle::GetArea()
{
return height*width;
}
Rectangle GetRect(int h,int w)
{
Rectangle rect;
rect.Set(h,w);
return rect;
}
int GetRectArea(Rectangle rect)
{
return rect.GetArea();
}
int main()
{
int h,w;
cin>>h>>w;
Rectangle rec=GetRect(h,w);
cout<<"长方形的面积为:"<
#include
#include
using namespace std;
class Student
{
public:
int SID;
string Name;
Student();
Student(int sid,string name);
~Student();
};
Student::Student()
{
SID = 0;
Name="王小明";
}
Student::Student(int sid,string name)
{
SID=sid;
Name=name;
}
Student::~Student()
{
cout<>i>>name1>>j>>name2>>k>>name3;
Student st1(i,name1);
Student st2(j,name2);
Student st3(k,name3);
{
Student st;
}
}
#include
#include
using namespace std;
class Student
{
public:
int Num;
string Name;
float Score;
Student();
Student(int sid,string name,float sco);
~Student();
};
Student::Student(int sid,string name,float sco)
{
Num=sid;
Name=name;
Score=sco;
}
Student::Student()
{}
Student s[5]={};
int people;
void Add(int sid,string name,float sco)
{
s[people]=Student(sid,name,sco);
people++;
}
void PrintAll()
{
int i;
for(i=0;i>i>>name1>>score1;
cin>>j>>name2>>score2;
cin>>k>>name3>>score3;
Add(i,name1,score1);
Add(j,name2,score2);
Add(k,name3,score3);
PrintAll();
Average();
}
#include
using namespace std;
class Int{
private:
int value;
public:
Int():value(0){}
Int(Int const&rhs):value(rhs.value){}
Int(int v):value(v){}
int getValue()const
{
return value;
}
};
void output(const Int&x)
{
cout<>x;
Int a(x);
output(a);
return 0;
}
#include
using namespace std;
class Int{
private:
int value;
public:
Int():value(0){}
Int(Int const&rhs):value(rhs.value){}
Int(int v):value(v){}
int getValue()const
{
return value;
}
void setValue(int v)
{
value=v;
}
};
void add(Int const&lhs,Int const&rhs,Int&ret)
{
ret=Int(lhs.getValue()+rhs.getValue());
}
void mul(Int const&lhs,Int const&rhs,Int&ret)
{
ret=Int(lhs.getValue()*rhs.getValue());
}
int main(){
int x,y;
cin>>x>>y;
Int a(x),b(y),c,d;
add(a,b,c);
mul(a,b,d);
cout<
#include
using namespace std;
class Int{
private:
int value;
public:
Int():value(0){}
Int(Int const&rhs):value(rhs.value){}
Int(int v):value(v){}
int getValue()const
{
return value;
}
};
Int add(const Int&lhs,const Int&rhs)
{
int x=lhs.getValue()+rhs.getValue();
Int a=Int(x);
return a;
}
Int mul(const Int&lhs,const Int&rhs)
{
int y=lhs.getValue()*rhs.getValue();
Int b=Int(y);
return b;
}
int main(){
int x,y;
cin>>x>>y;
Int a(x),b(y);
Int c=add(a,b);
Int d=mul(a,b);
cout<
#include
using namespace std;
class Student{
float score;
};
int main()
{
int sum;
int number;
int i;
cin>>sum;
float *p=new float[sum];
for(i=0;i>p[i];
}
cin>>number;
if(number<1||number>sum)
{
cout<<"Error";
}
else
{
cout<
#include
using namespace std;
int main()
{
//申请3行4列的二维数组内存
int sc; //班级总数
int ss; //每个班学生总数
cin>>sc;
int **score;
score=new int *[sc]; //先申请3个int*类型的一维指针,作为行
for (int q=0;q>ss;
score[q]=new int[ss];
for(int m=0;m>score[q][m];
}
int x,y;
cin>>x>>y;
cout<
#include
#include
using namespace std;
class Teacher;
class Student
{
private:
int number;
string name;
string sex;
friend class Teacher;
public:
Student(int num,string nam,string se)
{
number=num;
name=nam;
sex=se;
}
void Print()
{
cout<<"学生:"<> number >> name >> sex;
Student st(number,name,sex);
st.Print();
Teacher t = (Teacher)st;
t.Print();
}
/*
成员变量:这一部分学员可以自由发挥,但要求都是私有成员。
成员函数:
构造函数:Matrix(int r,int c),参数 r 和 c 分别代表矩阵的行和列。
全部设值函数:void Fill(int value),函数将矩阵内所有的元素都设置为参数 value 的值。
指定位置设值函数:void Set(int r,int c,int value),函数将矩阵第 r 行 c 列的元素设置为 value 的值。
获取元素函数:int Get(int r,int c)函数,函数返回矩阵第 r 行 c 列的元素。
打印函数:void Print(),函数按照矩阵的形状打印出矩阵内容,每一个值后跟着一个空格。
比如一个2x4元素全为1的矩阵,打印结果为(更明显表示格式,空格均用下划线_代替):
1_1_1_1_
1_1_1_1_
普通函数:
Matrix operator+(Matrix &m1,Matrix &m2)函数,重载Matrix类的加法运算符,实现矩阵的加法运算。
Matrix operator-(Matrix &m1,Matrix &m2)函数,重载Matrix类的减法运算符,实现矩阵的减法运算。
Matrix operator*(Matrix &m1,Matrix &m2)函数,重载Matrix类的乘法运算符,实现矩阵的乘法运算。
测试输入:1 1
预期输出:
m1 + m2 :
3
m1 - m2 :
-1
m1 * m3 :
1
测试输入:2 2
预期输出:
m1 + m2 :
3 3
3 3
m1 - m2 :
-1 -1
-1 -1
m1 * m3 :
1 2
1 2
*/
#include
#include
#define MAXHANG 20
#define MAXLIE 20
using namespace std;
class Matrix
{
private:
int x[MAXHANG][MAXLIE];
int hang;
int lie;
public:
Matrix(int r,int c);
Matrix(){}
void Fill(int value);
void Set(int r,int c,int value);
int Get(int r,int c);
void Print();
friend Matrix operator+(Matrix &m1,Matrix &m2);
friend Matrix operator-(Matrix &m1,Matrix &m2);
friend Matrix operator*(Matrix &m1,Matrix &m2);
};
Matrix::Matrix(int r,int c)
{
hang=r;
lie=c;
}
void Matrix::Fill(int value)
{
for(int i=0;i> i >> j;
Matrix m1(i,j),m2(i,j),m3(j,i);
m1.Fill(1);
m2.Fill(2);
m3.Fill(0);
for(int s=0;s
#include
using namespace std;
class Complex
{
friend Complex operator+(Complex &c1,Complex &c2);
friend Complex operator-(Complex &c1,Complex &c2);
friend Complex operator*(Complex &c1,Complex &c2);
public:
Complex()
{
real=0;
image=0;
}
Complex(float r,float i)
{
real=r;
image=i;
}
void Print();
private:
float real;
float image;
};
void Complex::Print()
{
if(image<0)
cout<> a >> b >> c >> d;
Complex c1(a,b),c2(c,d);
cout << "c1 = ";
c1.Print();
cout << "c2 = ";
c2.Print();
cout << "c1 + c2 = ";
(c1 + c2).Print();
cout << "c1 - c2 = ";
(c1 - c2).Print();
cout << "c1 * c2 = ";
(c1 * c2).Print();
}
#include
using namespace std;
class Shape
{
//基类的声明
public:
virtual void PrintArea()=0;
};
class Rectangle:public Shape
{
//矩形类的声明
public:
int width;
int height;
void PrintArea()
{
cout<<"矩形面积 = "<>i>>j;
Shape *ptr=new Rectangle(i,j);
ptr->PrintArea();
delete ptr;
ptr=new Circle(i);
ptr->PrintArea();
delete ptr;
return 0;
}
#include
using namespace std;
class Chinese
{
public:
virtual void greet();
};
void Chinese::greet()
{
cout<<"你好"<
#include
using namespace std;
class Repeater
{
//复读机基类的声明
public:
virtual void Play(){}
virtual ~Repeater()
{
cout<<"砰!"<>i;
Repeater *ptr=CreateRepeater(i);
ptr->Play();
delete ptr;
}
#include
#include
using namespace std;
class People
{
public:
string Name;
void PrintName();
};
void People::PrintName()
{
cout << "姓名:" << Name << endl;
}
//公有继承 People
class Student:public People
{
public:
int SID;
void PrintSID();
};
void Student::PrintSID()
{
//输出 SID
cout << "学号:" << SID << endl;
}
void Set(int sid,string name,Student *ptr)
{
ptr->Name=name;
ptr->SID=sid;
//给 ptr 对象的两个属性赋值
}
int main()
{
int id;
string name;
cin>>id>>name;
Student st;
Set(id,name,&st);
st.PrintSID();
st.PrintName();
}
#include
#include
using namespace std;
class People
{
public:
string Name;
void PrintName();
};
void People::PrintName()
{
cout << "姓名:" << Name << endl;
}
//保护继承 People
class Student:protected People
{
public:
int SID;
void PrintSID();
void set2(string name)
{
Name=name;
}
};
void Student::PrintSID()
{
cout<<"学号:"<set2(name);
ptr->SID=sid;
}
int main()
{
int id;
string name;
cin >> id >> name ;
Student st;
Set(id,name,&st);
st.PrintSID();
((People*)&st)->PrintName();
}
#include
#include
using namespace std;
class People
{
public:
string Name;
void PrintName();
};
void People::PrintName()
{
cout<<"姓名:"<set2(name);
ptr->setsid(sid);
ptr->ResearchID=rid;
}
int main()
{
int i,j;
string name;
cin>>i>>j>>name;
Graduate st;
Set(name,i,j,&st);
((Student*)&st)->PrintSID();
((People*)&st)->PrintName();
st.PrintResearchID();
}
#include
#include
using namespace std;
class Wolf
{
public:
string Name;
int Shape;
void PrintState()
{
cout<<"姓名:"<>i>>j>>name;
Werewolf ww;
ww.SetName(name);
ww.SetState(i,j);
ww.PrintAllState();
}
#include
#include
using namespace std;
class User
{
public:
string Name;
int Books;
User(string name,int books);
~User();
static void GetState();
static int UserCount;
static int BookCount;
};
int User::UserCount=0;
int User::BookCount=0;
User::User(string name,int books)
{
Name=name;
Books=books;
cout<> name1 >> a >> name2 >> b >> name3 >> c ;
User *u1 = new User(name1,a);
User *u2 = new User(name2,b);
User::GetState();
delete u1;
u1 = new User(name3,c);
User::GetState();
delete u2;
User::GetState();
delete u1;
User::GetState();
return 0;
}