目录
一、类和对象的创建和使用:
1、设计学生信息类
#include <iostream>
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<<endl;
cout<<"姓名:"<<Name<<endl;
cout<<"班级:"<<Class<<endl;
cout<<"手机号:"<<Phone<<endl;
}
int main()
{
int SID;
string str1,str2,str3;
cin >>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;
}
2、汽车类
#include<iostream>
#include<string.h>
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<<"车门 "<<bwm.door<<endl;
cout<<"车灯 "<<bwm.light<<endl;
cout<<"速度 "<<bwm.speed<<endl;
return 0;
}
3、长方形类
#include<iostream>
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<<"长方形的面积为:"<<GetRectArea(rec)<<endl;
}
二、构造函数与析构函数:
1、学生信息类
#include<iostream>
#include<string.h>
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<<SID<<" "<< Name<<" 退出程序"<<endl;
}
int main()
{
int i,j,k;
string name1,name2,name3;
cin>>i>>name1>>j>>name2>>k>>name3;
Student st1(i,name1);
Student st2(j,name2);
Student st3(k,name3);
{
Student st;
}
}
2、对象数组-学生信息表
#include <string>
#include <iostream>
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<people;i++)
{
cout<<s[i].Num<<" "<<s[i].Name<<" "<<s[i].Score<<endl;
}
}
void Average()
{
int i;
float sum=0;
float average=0;
for(i=0;i<people;i++)
{
sum+=s[i].Score;
}
average=sum/people;
cout<<"平均成绩 "<<average<<endl;
}
Student::~Student()
{}
int main()
{
int i,j,k;
string name1,name2,name3;
float score1,score2,score3;
cin>>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<iostream>
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.getValue()<<endl;
}
int main(){
int x;
cin>>x;
Int a(x);
output(a);
return 0;
}
四、类对象作为输出参数
#include<iostream>
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<<c.getValue()<<" "<<d.getValue()<<endl;
return 0;
}
五、对象作为函数返回值
#include<iostream>
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<<c.getValue()<<" "<<d.getValue()<<endl;
return 0;
}
六、动态内存分配:
1、成绩处理
#include <iostream>
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<sum;i++)
{
cin>>p[i];
}
cin>>number;
if(number<1||number>sum)
{
cout<<"Error";
}
else
{
cout<<p[number-1]<<endl;
}
delete []p;
}
2、统计学生成绩
#include <iostream>
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<sc;++q)
{ //再为每一行申请一个 拥有4个int型空间 的数组
cin>>ss;
score[q]=new int[ss];
for(int m=0;m<ss;++m)
cin>>score[q][m];
}
int x,y;
cin>>x>>y;
cout<<score[x][y];
for(int i=0;i<sc;++i)
{
delete []score[i];/*释放3个指针*/
}
delete []score;
return 0;
}
七、运算符重载与友元函数:
1、学生信息转换
#include <iostream>
#include <string>
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<<"学生:"<<name<<",编号:"<<number<<",性别:"<<sex<<endl;
}
};
class Teacher
{
private:
int number;
string name;
string sex;
public:
void Print()
{
cout<<"教师:"<<name<<",编号:"<<number<<",性别:"<<sex;
}
Teacher(const Student &t)
{
number=t.number;
name=t.name;
sex=t.sex;
}
};
int main()
{
int number;
string name,sex;
cin >> number >> name >> sex;
Student st(number,name,sex);
st.Print();
Teacher t = (Teacher)st;
t.Print();
}
2、矩阵运算
/*
成员变量:这一部分学员可以自由发挥,但要求都是私有成员。
成员函数:
构造函数: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 <iostream>
#include <string>
#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<hang;i++)
for(int j=0;j<lie;j++)
x[i][j]=value;
}
void Matrix::Set(int r,int c,int value)
{
x[r][c]=value;
}
void Matrix::Print()
{
for(int i=0;i<hang;i++){
for(int j=0;j<lie;j++)
{
cout<<x[i][j]<<" ";
}
cout<<endl;
}
}
int Matrix::Get(int r,int c)
{
return x[r][c];
}
Matrix operator+(Matrix& m1,Matrix &m2)
{
Matrix m3(m1.hang,m1.lie);
for(int i=0;i<m1.hang;i++){
for(int j=0;j<m2.lie;j++)
{
m3.x[i][j]=m1.x[i][j]+m2.x[i][j];
}
}
return m3;
}
Matrix operator-(Matrix& m1,Matrix &m2)
{
Matrix m3(m1.hang,m1.lie);
for(int i=0;i<m1.hang;i++){
for(int j=0;j<m1.lie;j++)
{
m3.x[i][j]=m1.x[i][j]-m2.x[i][j];
}
}
return m3;
}
Matrix operator*(Matrix& m1,Matrix &m2)
{
Matrix m3(m1.hang,m2.lie);
for(int i=0;i<m1.hang;i++){
for(int j=0;j<m2.lie;j++){
int sum=0;
for(int k=0;k<m1.hang;k++){
sum+=m1.x[i][k]*m2.x[k][j]; //累加第i行与第j列的元素乘积
}
m3.x[i][j]=sum;//赋给i,j元素
}
}
return m3;
}
int main()
{
int i,j;
cin >> 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<i;s++){
for(int c=0;c<j;c++){
if(s==c)
m3.Set(s,c,s+1);
}
}
m1.Print();
m2.Print();
m3.Print();
cout<<"m1 + m2 :"<<endl;
(m1 + m2).Print();
cout<<"m1 - m2 :"<<endl;
(m1 - m2).Print();
cout<<"m1 * m3 :"<<endl;
(m1 * m3).Print();
}
3、复数运算
#include <iostream>
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<<real<<image<<"i"<<endl;
else
cout<<real<<"+"<<image<<"i"<<endl;
}
Complex operator+(Complex &c1,Complex &c2)
{
Complex c;
c.real=c1.real+c2.real;
c.image=c1.image+c2.image;
return c;
}
Complex operator-(Complex &c1,Complex &c2)
{
Complex c;
c.real=c1.real-c2.real;
c.image=c1.image-c2.image;
return c;
}
Complex operator*(Complex &c1,Complex &c2)
{
Complex c;
c.real=c1.real*c2.real-c1.image*c2.image;
c.image=c1.image*c2.real+c1.real*c2.image;
return c;
}
int main()
{
float a,b,c,d;
cin >> 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();
}
八、类的多态性和虚函数:
1、计算图像面积
#include<iostream>
using namespace std;
class Shape
{
//基类的声明
public:
virtual void PrintArea()=0;
};
class Rectangle:public Shape
{
//矩形类的声明
public:
int width;
int height;
void PrintArea()
{
cout<<"矩形面积 = "<<width*height<<endl;
}
Rectangle(float w,float h)
{
width=w;
height=h;
}
};
class Circle:public Shape
{
//圆形类的声明
public:
float radio;
void PrintArea()
{
cout<<"圆形面积 = "<<radio*radio*3.14<<endl;
}
Circle(float r)
{
radio=r;
}
};
int main()
{
int i,j;
cin>>i>>j;
Shape *ptr=new Rectangle(i,j);
ptr->PrintArea();
delete ptr;
ptr=new Circle(i);
ptr->PrintArea();
delete ptr;
return 0;
}
2、人与复读机
#include<iostream>
using namespace std;
class Chinese
{
public:
virtual void greet();
};
void Chinese::greet()
{
cout<<"你好"<<endl;
}
class EnglishLearner:public Chinese
{
//英语学生类的声明
public:
void greet()
{
cout<<"Hello"<<endl;
}
};
class Repeater:public Chinese
{
//复读机类的声明
public:
void greet()
{
Chinese::greet();
}
};
int main()
{
Chinese ce;
EnglishLearner le;
Repeater re;
ce.greet();
le.greet();
re.greet();
}
3、复读机的毁灭
#include<iostream>
using namespace std;
class Repeater
{
//复读机基类的声明
public:
virtual void Play(){}
virtual ~Repeater()
{
cout<<"砰!"<<endl;
}
};
class ForRepeater:public Repeater
{
//正向复读机的声明
public:
void Play()
{
cout<<"没想到你也是一个复读机"<<endl;
}
~ForRepeater()
{
cout<<"正·复读机 炸了"<<endl;
}
};
class RevRepeater:public Repeater
{
//反向复读机的声明
public:
void Play()
{
cout<<"没机读复个一是也你到想没"<<endl;
}
~RevRepeater()
{
cout<<"机读复·反 炸了"<<endl;
}
};
Repeater* CreateRepeater(int type)
{
//根据type创建指定的复读机
Repeater* p;
if(type==0)
{
p=new ForRepeater;
return p;
}
else if(type==1)
{
p=new RevRepeater;
return p;
}
return 0;
}
int main()
{
int i;
cin>>i;
Repeater *ptr=CreateRepeater(i);
ptr->Play();
delete ptr;
}
九、类的继承与派生:
1、公有继承
#include <string>
#include <iostream>
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();
}
2、保护继承
#include <string>
#include <iostream>
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<<"学号:"<<SID<<endl;
}
void Set(int sid,string name,Student *ptr)
{
ptr->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();
}
3、私有继承
#include <string>
#include <iostream>
using namespace std;
class People
{
public:
string Name;
void PrintName();
};
void People::PrintName()
{
cout<<"姓名:"<<Name<<endl;
}
//私有继承 People 类
class Student:private People
{
public:
int SID;
void PrintSID();
//添加一个 Set 函数来设置父类的 Name 成员
void SetName(string name)
{
Name=name;
}
};
void Student::PrintSID()
{
//输出学号 SID
cout<<"学号:"<<SID<<endl;
}
//公有继承 Student类
class Graduate:public Student
{
public:
int ResearchID;
void PrintResearchID();
//添加一个 Set 函数来设置父类的 SID 成员
void setsid(int sid)
{
SID=sid;
}
//添加一个 Set 函数来调用父类的 SetName 函数
void set2(string name)
{
SetName(name);
}
};
void Graduate::PrintResearchID()
{
cout<<"研究方向:"<<ResearchID<<endl;
//输出研究方向 ResearchID
}
void Set(string name,int sid,int rid,Graduate *ptr)
{
//设置ptr所指对象的三个成员
ptr->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();
}
4、多继承
#include <string>
#include <iostream>
using namespace std;
class Wolf
{
public:
string Name;
int Shape;
void PrintState()
{
cout<<"姓名:"<<Name<<",爪子锋利度为:"<<Shape<<endl;
}
};
class Human
{
public:
string Name;
int Intell;
void PrintState()
{
cout<<"姓名:"<<Name<<",智力为:"<<Intell<<endl;
}
};
class Werewolf:public Wolf,public Human
{
public:
void SetName(string name);
void SetState(int shape,int intell);
void PrintAllState();
};
void Werewolf::SetName(string name)
{
Wolf::Name=name;
Human::Name=name;
}
void Werewolf::SetState(int shape,int intell)
{
Wolf::Shape=shape;
Human::Intell=intell;
}
void Werewolf::PrintAllState()
{
Wolf::PrintState();
Human::PrintState();
}
int main()
{
int i,j;
string name;
cin>>i>>j>>name;
Werewolf ww;
ww.SetName(name);
ww.SetState(i,j);
ww.PrintAllState();
}
十、静态成员-模拟共享书店
#include <string>
#include <iostream>
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<<Name<<" "<<Books<<" 进入"<<endl;
User::UserCount++;
User::BookCount+=books;
}
User::~User()
{
cout<<Name<<" "<<Books<<" 离开"<<endl;
User::UserCount--;
User::BookCount-=Books;
}
void User::GetState()
{
cout<<"书店人数:"<<User::UserCount<<","<<"书店共享书数量:"<<User::BookCount<<","<<"人均共享数量:"<<User::BookCount/User::UserCount<<endl;
}
int main()
{
string name1,name2,name3;
int a,b,c;
cin >> 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;
}