//112
#include<iostream>
using namespace std;
class yuan
{
public:
double area(double r)
{
return r*r*3.14;
}
double l(double r)
{
return 2*3.14*r;
}
public:
double r;
};
void main()
{
yuan y;
cout<<"输入半径:";
cin>>y.r;
cout<<"圆的面积:"<<y.area(y.r)<<endl;
cout<<"圆的周长:"<<y.l(y.r)<<endl;
}
//113 类的继承
#include<iostream>
using namespace std;
class animal
{
public:
animal(){}
~animal(){}
void respire()
{
cout<<"huxi"<<endl;
}
};
class A:public animal
{
public:
A(){}
~A(){}
void aaa()
{
cout<<"xingzou"<<endl;
}
};
class B:public A
{
public:
B(){}
~B(){}
void think()
{
cout<<"think"<<endl;
}
};
void main()
{
B b;
cout<<"b";
b.respire();
b.aaa();
b.think();
}
//114 员工月薪发放 多态
//类的多态性体现在当响应同一条消息时,结果却不一样,也即结果的多样化
//类多态的应用存在于基类的指针指向子类的对象。
//虚成员函数用关键字virtual标识,友元函数用关键字friend标识
#include <iostream>
using namespace std;
class salary
{
public:
salary(){}
~salary(){}
virtual void pay(){}
private:
};
class member1:public salary
{
public:
member1(double a){_total=a;}
~member1(){}
void pay()
{
cout<<"A pay:"<<_total<<endl;
}
private:
double _total;
};
class B:public salary
{
public:
B(double a){_total=a;}
~B(){}
void pay()
{
cout<<"B pay"<<_total<<endl;
}
private:
double _total;
};
void main()
{
member1 m1(40000.00);
B b(300.00);
salary *sa1=&m1;
salary *sa2=&b;
sa1->pay();
sa2->pay();
}
//115 家族性格遗传(纯虚函数)
//下一代的性格是从上一代继承而来的
#include<iostream>
using namespace std;
class xingge
{
public:
xingge(){}
virtual void talk()=0;
virtual void kind_hearted()=0;
};
class child1:public xingge
{
public:
child1(){}
void talk()
{
cout<<"chengmo"<<endl;
}
void kind_hearted()
{
cout<<"rexin"<<endl;
}
};
class child2:public xingge
{
public :
child2(){}
void talk()
{
cout<<"huduo"<<endl;
}
void kind_hearted()
{
cout<<"lenmo"<<endl;
}
};
void main()
{
xingge *xg;
child1 ch1;
child2 ch2;
xg=&ch1;
cout<<"ch1de xing ge:"<<endl;
xg->talk();
xg->kind_hearted();
xg=&ch2;
cout<<"ch2de cing ge:"<<endl;
xg->talk();
xg->kind_hearted();
}
//116 比谁跑得快 类+算法
#include<iostream>
using namespace std;
class speed
{
public:
speed(double a,double b)
{
_time=a;
_distance=b;
}
~speed(){}
double cal_speed()
{
return _distance/_time;
}
private:
double _time;
double _distance;
};
void main()
{
speed v1(5,100);
speed v2(5,200);
if(v1.cal_speed()>v2.cal_speed())
{
cout<<"di yi ge kuai"<<endl;
}
else if(v1.cal_speed()<v2.cal_speed())
{
cout<<"di er ge kuai"<<endl;
}
else
{
cout<<"yi yang kuai"<<endl;
}
}
//117 错误的模糊引用 类继承问题 二义性问题可以通过虚继承解决
#include<iostream>
using namespace std;
class human
{
public:
bool getBeauty()
{
return m_beauty;
}
bool m_beauty;
};
class Chinese:virtual public human
{
};
class woman:virtual public human
{
};
class me:public Chinese,public woman
{
public:
me(bool a)
{
m_beauty=a;
}
};
void main()
{
me m(true);
cout<<m.getBeauty()<<endl;
}
//118 实现类自动化管理内存
//自动化管理内存,就是相对于程序员手动分配和释放内存块来说,系统自动完成这些任务
#include<iostream>
using namespace std;
class memory
{
public:
memory(int length)
{
_length=length;
memo=new int[_length];
for(int i=0;i<_length;i++)
memo[i]=0;
cout<<"fen pei nei cun cheng gong"<<endl;
}
~memory()
{
delete []memo;
if (memo!=NULL)
memo=NULL;
cout<<"nei cun shi fang cheng gong"<<endl;
}
void output()
{
for (int i=0;i<_length;i++)
{
cout<<memo[i]<<" ";
}
}
private:
int *memo;
int _length;
};
void main()
{
memory _mem(20);
_mem.output();
}
//119 入学登记系统(类+算法+综合)
#include<iostream>
#include<string>
using namespace std;
struct stu_info
{
string name;
int sex;
int age;
string hometown;
string mobile_number;
int major;
int class_num;
};
class student
{
public:
student();
void prin();
void input();
bool certain();
void registers();
private:
stu_info stu[100];
stu_info temp;
int num;
};
student::student()
{
num=0;
}
void student::prin()
{
cout<<"本系统只登记电子信息学院的学生信息"<<endl;
cout<<"学院的专业如下:"<<endl;
cout<<"1-电子信息 2-通信"<<endl;
cout<<"每个专业下设两个班,分别为00和01"<<endl;
}
void student::input()
{
cout<<"请按照下面提示,在右面输入信息:"<<endl;
cout<<"姓名:"<<endl;cin>>temp.name;
cout<<"性别:"<<endl;cin>>temp.sex;
cout<<"年龄:"<<endl;cin>>temp.age;
cout<<"籍贯:"<<endl;cin>>temp.hometown;
cout<<"手机号码:"<<endl;cin>>temp.mobile_number;
cout<<"专业编号:"<<endl;cin>>temp.major;
cout<<"班级编号:"<<endl;cin>>temp.class_num;
}
bool student::certain()
{
for (int i=0;i<num;i++)
{
if (stu[i].age==temp.age &&
stu[i].class_num==temp.class_num &&
stu[i].hometown==temp.hometown &&
stu[i].major==temp.major &&
stu[i].mobile_number==temp.mobile_number&&
stu[i].name==temp.name&&
stu[i].sex==temp.sex)
{
return false;
}
}
return true;
}
void student::registers()
{
if (certain()==true)
{
if (num>99)
{
cout<<"登记系统已满,下午再来"<<endl;
}
else
{
stu[num]=temp;
cout<<"登记成功"<<endl;
}
}
else
{
cout<<"您已登记"<<endl;
}
}
void main()
{
student a;
int ch;
a.prin();
do{
a.input();
a.registers();
cout<<"继续输入吗:0-是,1-否"<<endl;
cin>>ch;
}while(ch==0);
}
//120 判断一个点是否位于矩形内
#include <iostream>
using namespace std;
class juxing
{
public:
juxing();
void juxingdaxiao();
void juagement(double a,double b);
void jixu();
bool a;
private:
double kuan;
double chang;
};
juxing::juxing()
{
};
void juxing::juxingdaxiao()
{
cout<<"矩形的宽:";
cin>>kuan;
cout<<kuan<<endl;
cout<<"矩形的长:";
cin>>chang;
cout<<chang<<endl;
};
void juxing:: juagement(double a,double b)
{
if (a<=kuan&&b<=chang)
{
cout<<"点在矩形内"<<endl;
}
else
{
cout<<"点在矩形外"<<endl;
}
}
void juxing::jixu()
{
cout<<"是否继续:0-yes 1-no"<<endl;
cin>>a;
}
void main()
{
juxing A;
double a,b;
do
{
A.juxingdaxiao();
cout<<"请输入判断的点:";
cin>>a>>b;
A.juagement(a,b);
A.jixu();
}while(A.a==0);
}
//121 学生的假期生活(接口)
#include <iostream>
using namespace std;
class vocation
{
public:
vocation(){}
vocation(double a,double b,double c);
~vocation(){}
void seteat(double a);
double geteattime();//接口1,获得吃饭时间
void sleep(double b);
double getsleeptime();
void paly(double c);
double getplaytime();
private:
double eattime;
double sleeptime;
double playtime;
};
vocation::vocation(double a,double b,double c)
{
eattime=a;
sleeptime=b;
playtime=c;
};
void vocation::seteat(double a)
{
eattime=a;
};
double vocation::geteattime()
{
return eattime;
};
void vocation::paly(double c)
{
playtime=c;
};
double vocation::getplaytime()
{
return playtime;
};
void vocation::sleep(double b)
{
sleeptime=b;
};
double vocation::getsleeptime()
{
return sleeptime;
};
void main()
{
double a,b,c;
vocation va(5.6,8.0,3.0);
cout<<"前一天吃饭睡觉玩耍所花费的时间为:";
cout<<va.geteattime()<<" "<<va.getsleeptime()<<" "<<va.getplaytime()<<endl;
cout<<"请按下面提示输入今天花费的时间:"<<endl;
cout<<"吃饭:";
cin>>a;
cout<<"睡觉";
cin>>b;
cout<<"玩耍";
cin>>c;
if (a+b+c>24)
{
cout<<"setting false"<<endl;
}
else
{
if (a>4.5)
{
cout<<"饭桶"<<endl;
}
va.seteat(a);
if (b>12)
{
cout<<"睡神"<<endl;
}
va.sleep(b);
if (c>6)
{
cout<<"网瘾少年"<<endl;
}
va.paly(c);
}
}
//122 判断一个矩形是否成立
#include <iostream>
using namespace std;
class juxing
{
public:
//juxing(){}
~juxing(){}
void input();
void judage();
bool output;
private:
int a,b,c,d,e,f,g,h;
};
//juxing::juxing()
//{
//};
void juxing::input()
{
cout<<"输入四个点的坐标并以空格隔开:";
cin>>a>>b>>c>>d>>e>>f>>g>>h;
};
void juxing::judage()
{
int num=0;
if ((a-c)*(b-d)+(a-e)*(b-f)==0)
{
num++;
}
if((c-a)*(d-b)+(c-g)*(d-h)==0)
{
num++;
}
if((e-a)*(f-b)+(e-g)*(f-h)==0)
{
num++;
}
if (num==3)
{
output=true;
}
else
{
output=false;
}
};
void main()
{
juxing A;
A.input();
A.judage();
if (A.output==true)
{
cout<<"是矩形"<<endl;
}
else
{
cout<<"不是矩形"<<endl;
}
}
//123 类的静态成员变量应用(对象间数据共享)
//在非类的编程中,可以声明全局变量实现数据的共享。然而在类中,全局变量的使用具有局限性
//此时可以定义为静态数据成员实现多个对象间的数据共享,
#include<iostream>
using namespace std;
class Apple
{
private:
static int apples;
int apple;
public:
Apple(){apple=5;}
void setApples(int a,int b)
{
apples=a;
apple=b;
}
static int getApples()
{
return apples;
}
int getApple()
{
return apple;
}
};
int Apple::apples=10;
void main()
{
cout<<"未定义APPLE类对象之前,其静态成员apples="<<Apple::getApples()<<endl;
Apple a,b;
cout<<"未赋值前,两个对象的各自变量值如下:"<<endl;
cout<<"a.apples="<<a.getApples()<<endl;
cout<<"a.apple="<<a.getApple()<<endl;
cout<<"b.apples="<<b.getApples()<<endl;
cout<<"b.apple="<<b.getApple()<<endl;
a.setApples(50,100);
cout<<"赋值后,两个对象的各自变量值如下:"<<endl;
cout<<"a.apples="<<a.getApples()<<endl;
cout<<"a.apple="<<a.getApple()<<endl;
cout<<"b.apples="<<b.getApples()<<endl;
cout<<"b.apple="<<b.getApple()<<endl;
}
//124 获取系统时间
#include <sys/timeb.h>
#include<time.h>
#include<iostream>
using namespace std;
class TIME
{
private:
double timeM;//总时间,换算为毫秒
int time_H;
int time_M;
int time_S;
double millisecond;//毫秒
public:
void cal();//计算当前时间
double getSystime();//获取总时间
int getH();
int getM();
int getS();
};
void TIME::cal()
{
struct _timeb timebuffer;//时间缓冲变量
char *timeline;
unsigned short millitml;
char temp_H[2];
char temp_M[2];
char temp_S[2];
_ftime(&timebuffer);//获取缓冲变量
timeline=ctime(&(timebuffer.time));//转换为char*型
millitml=timebuffer.millitm;
millisecond=(double)millitml/1000;//变换为真正的毫秒
for(int i=0;i<2;i++)
{
temp_H[i]=timeline[i+11];//从第十一字节开始为时的字符值,下同
}
for (int j=0;j<2;j++)
{
temp_M[j]=timeline[j+14];
}
for (int k=0;k<2;k++)
{
temp_S[k]=timeline[k+17];
}
time_H=atoi(temp_H);//时变为整型
time_M=atoi(temp_M);
time_S=atoi(temp_S);
timeM=double(time_H)*3600+double(time_M)*60+double(time_S)+millisecond;
}
double TIME::getSystime()
{
return timeM;
}
int TIME::getH()
{
return time_H;
}
int TIME::getM()
{
return time_M;
}
int TIME::getS()
{
return time_S;
}
void main()
{
TIME t;
t.cal();
cout<<"当前系统时间:"<<t.getSystime()<<endl;
cout<<"时:"<<t.getH()<<endl;
cout<<"分:"<<t.getM()<<endl;
cout<<"秒:"<<t.getS()<<endl;
}
//125 内联函数应用于计算两点之间的距离
//在类的内部被声明又被定义的函数称为内联函数,它的应用是解决函数的调用效率问题
#include<iostream>
using namespace std;
class Line
{
private:
int x1,y1,x2,y2;
public:
Line();
inline Line(int a=0,int b=0,int c=0,int d=0);
inline void printPoint();
inline double getDis();
};
inline Line::Line(int a,int b,int c,int d)
{
x1=a;
y1=b;
x2=c;
y2=d;
};
inline void Line::printPoint()
{
cout<<"A:("<<x1<<","<<y1<<")"<<endl;
cout<<"B:("<<x2<<","<<y2<<")"<<endl;
};
inline double Line::getDis()
{
double dis;
dis=sqrt(double(((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))));
return dis;
};
void main()
{
Line line(10,68,-5,6);
line.printPoint();
cout<<"两点之间的距离:"<<line.getDis()<<endl;
}
//126 this指针的应用
//每个类对象在实例化的时候都有一个this指针指向其数据
//的首地址。当类的费静态数据成员函数在访问费静态成员变量时,
//若遇到形变参量和成员变量形同,可以使用this指针指向成员变量
//以示区别
#include<iostream>
using namespace std;
class A
{
private:
int num;
public:
A(int num)
{
this->num=num;
}
void output()
{
cout<<"私有成员变量num="<<num<<endl;
}
};
void main()
{
A a(10);
a.output();
}
//127 复制构造函数的应用(赋值矩阵)
//本实例实现使用复制构造函数从一个类对象复制另一个对象,可从屏幕输入矩阵元素,
//调用拷贝构造函数后,创建另一个对象,并初始化其成员变量
#include<iostream>
#include<math.h>
#include<assert.h>
using namespace std;
class CMatrix
{
public:
CMatrix();
CMatrix(int nRow,int nCol,int* pAry);
CMatrix(CMatrix &src);//复制构造函数
~CMatrix();
void output();
int Row()const{return m;};
int Col()const{return n;};
private:
int m;
int n;
int** m_pData;//矩阵
};
CMatrix::CMatrix()
{
m=0;
n=0;
m_pData=NULL;
};
CMatrix::CMatrix(int nRow,int nCol,int *pAry)
{
m=nRow;
n=nCol;
m_pData=new int*[m];
for (int i=0;i<m;i++)
{
m_pData[i]=new int[n];
for (int j=0;j<n;j++)
{
m_pData[i][j]=pAry[i*n+j];
}
}
};
CMatrix::CMatrix(CMatrix &src)//复制构造函数
{
m=src.Row();
n=src.Col();
m_pData=new int *[src.Row()];
for (int i=0;i<src.Row();i++)
{
m_pData[i]=new int[src.Col()];
for (int j=0;j<src.Col();j++)
{
m_pData[i][j]=src.m_pData[i][j];
}
}
};
void CMatrix::output()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<m_pData[i][j]<<" ";
}
cout<<endl;
}
};
CMatrix::~CMatrix()
{
if (m_pData!=NULL)
{
for(int i=0;i<m;i++)
{
delete[] m_pData[i];
}
delete[] m_pData;
m_pData=NULL;
}
m=0;
n=0;
};
void main()
{
int m,n;
int *data;
cout<<"请输入矩阵的行数:";
cin>>m;
cout<<"请输入矩阵的列数:";
cin>>n;
data=new int[n*m];
cout<<"请初始化"<<m<<"行"<<n<<"列矩阵"<<endl;
for (int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>data[i*n+j];
}
}
CMatrix m1(m,n,data);
CMatrix m2(m1);
cout<<"使用复制构造函数后,m2对象的矩阵为:"<<endl;
m2.output();
}
//128 走出迷宫 (类+算法)
#include <iostream>
using namespace std;
#define M 10
#define N 10
class Maze
{
public:
Maze();
~Maze(){}//后面有花括号的构造函数和析构函数不用加分号
void PrintMaze();//打印迷宫
void GetPos();
void SearchMaze();
bool EEE(int i,int j);
private:
int maze[M][N];
int start_row;
int start_col;
int end_row;
int end_col;
bool succeed;
};
Maze::Maze()
{
int copy[M][N]=
{
1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,1,0,1,0,1,0,0,1,
1,0,0,1,0,1,1,1,0,1,
1,1,0,1,0,1,1,0,0,0,
1,0,0,0,0,0,1,0,0,1,
1,1,1,1,1,1,1,0,1,1,
1,1,0,1,0,1,1,0,0,1,
1,0,0,0,0,0,1,1,0,1,
1,1,1,1,1,1,1,1,1,1,
};
memcpy(maze,copy,sizeof(maze));
succeed=false;
};
void Maze::PrintMaze()
{
bool break_flag=0;
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
if (maze[i][j]==1)
{
cout<<"o ";
}
else if(maze[i][j]==0)
{
cout<<" ";
}
else
{
cout<<"矩阵元素错误"<<endl;
break_flag=1;
break;
}
}
if (break_flag==1)
{
break;
}
else
{
cout<<endl;
}
}
};
void Maze::GetPos()
{
cout<<"请输入起点的行:";
cin>>start_row;
cout<<"请输入起点的列:";
cin>>start_col;
cout<<"请输入终点的行:";
cin>>end_row;
cout<<"请输入终点的列:";
cin>>end_col;
};
void Maze::SearchMaze()
{
//if (maze[start_row][start_row]==1 || maze[end_row][end_col]==1)
//{
// cout<<"起点、终点输入错误"<<endl;
//}
//else
{
cout<<"jinru xunlu"<<endl;
if (EEE(start_row,start_col)==false)
{
cout<<"unsucceed"<<endl;
}
else
{
cout<<endl<<"显示路径"<<endl;
for (int i=0;i<M;i++)
{
for (int j=0;j<N;j++)
{
if (maze[i][j]==1)
{
cout<<"o ";
}
else if(maze[i][j]==2)
{
cout<<"* ";
}
else
{
cout<<" ";
}
}
cout<<endl;
}
}
}
};
bool Maze::EEE(int i,int j)
{
maze[i][j]=2;
cout<<i<<" "<<j;
if (i==end_row && j==end_col)
{
cout<<"succeed"<<endl;
succeed=true;
}
if (succeed!=true && maze[i][j+1]==0)
{
EEE(i,j+1);
}
if (succeed!=true && maze[i][j-1]==0)
{
EEE(i,j-1);
}
if (succeed!=true && maze[i-1][j]==0)
{
EEE(i-1,j);
}
if (succeed!=true && maze[i+1][j]==0)
{
EEE(i+1,j);
}
if(succeed!=true)
{
maze[i][j]=0;
}
return succeed;
};
void main()
{
Maze A;
A.PrintMaze();
A.GetPos();
A.SearchMaze();
}