实验内容
1.编写一个Circle类,主调函数如下:
int main()
{
float radius;
cout<<"请输入圆的半径:";
cin>>radius;
Circle obj1(radius);
Circle obj2(obj1);
cout<<"对象1的面积为:"<<obj1.GetArea()<<endl;
cout<<"对象2的面积为:"<<obj2.GetArea()<<endl;
return(0);
}
当程序运行后输入圆半径3.6之后,屏幕显示如下:
请输入圆的半径:3.6
构造函数被调用
拷贝构造函数被调用
对象1的面积为:40.6944
对象2的面积为:40.6944
析构函数被调用!
析构函数被调用!
2.编写一个Student类,主调函数如下:
int main()
{
Student stud1(2017001,"John");
stud1.display();
return 0;
}
程序运行后的结果是:
执行Student类构造函数
学号: 2017001
姓名: John
执行Student类析构函数
3.设计一个银行账户(Account)类,包含户名、帐号以及当前余额属性,可完成开户、存款、取款和查询余额等行为。银行账户类的定义如下:
class Account
{
public:
Account(char name[],long num,float amount); //类的有参构造函数
Account(); //类的无参构造函数
void deposit(float amount); //往账户中存款
int withdraw(float amount); //从账户中取款(考虑透支情况)
float getBalance(); //查询余额
private:
char mName[20]; //银行账户的户名
long mSN; //本账户的帐号
float mBalance; //本账户当前的余额
};
请根据上述给定的类,完善其相应的构造函数及成员函数的定义,并执行主函数实现测试。
4.设计一个股票(Stock)类,包含股票名称、股票代码、股票的数量、每股的价格、股票的总值等属性,可完成获得股票(用构造函数完成)、增持、卖出股票、更新股票价格、显示所持股票的信息等行为。
5.建立类Cylinder(圆柱体类),在该类中包括:
(1)成员变量:radius(圆柱体的半径),height(高度),volume(体积);
(2)带两个参数的构造函数,分别传值给圆柱体的半径和高度。
(3)一个成员函数printVolume( ),用来显示每个cylinder对象的体积。
(4)编写main函数进行类的测试。
6.定义一个Book(图书)类,在该类定义中包括:
(1) 成员变量:bookname(书名)、price(价格) 和number(存书数量)。
(2) 成员函数:display()显示图书的情况;borrow()将存书数量减1。并显示当前存书数量;restore( )将存书数量加1,并显示当前存书数量。
(3)定义合适的构造函数。
(4)在main函数中,要求建立某几种图书对象,并对该图书进行简单的显示、借阅和归还管理。
已知测试函数如下:
int main()
{
Book b1("English",20,3), b2("math",24,5);
b1.display();
b1.borrow();
b2.display();
b2.restore();
return 0;
}
7.定义一个MyString类,实现两个字符串连接。要求定义类的数据成员为字符指针,实现深拷贝函数。要求:不能使用<string>,可以使用<cstring> strcpy strcat strcmp strlen( )
已知测试函数如下:
int main()
{
MyString str1("Hello");
MyString str2(" World!");
MyString str3(str1);
MyString str4;
str1.display();
str2.display();
str3.display();
cout<<endl;
str1.MyStrcat(str2);
str2.MyStrcat(str1);
str4.MyStrcat(str1);
str4.MyStrcat(str2);
str1.display();
str2.display();
str3.display();
str4.display();
cout<<endl;
return 0;
}
实验结果
本次实验主要是考察类与对象的相关知识,程序的原理类似,这里只粘了部分实验结果
第3题

第4题

第7题


第7题按照要求不能使用字符串相加,采用的方法是将原来的字符串str复制给新的字符串s1,然后new出一个长度为str和传入的字符串s长度之和的新字符串赋给原来的str,最后s1写入str的前面,s写到s1的后面完成原字符串str和传入的字符串s的拼接

源代码
第1题
#include<iostream>
using namespace std;
class Circle
{
public:
Circle(float r);
Circle(Circle&b);
float GetArea();
~Circle();
private:
float a,s;
};
Circle::Circle(float r)
{
cout<<"构造函数被调用"<<endl;
a=r;
}
Circle::Circle(Circle&b)
{
cout<<"拷贝构造函数被调用"<<endl;
a=b.a;
}
float Circle::GetArea()//计算圆的面积
{
s=3.14*a*a;
return s;
}
Circle::~Circle()
{
cout<<"析构函数被调用!"<<endl;
}
int main()
{
float radius;
cout<<"请输入圆的半径:";
cin>>radius;
Circle obj1(radius);
Circle obj2(obj1);
cout<<"对象1的面积为:"<<obj1.GetArea()<<endl;
cout<<"对象2的面积为:"<<obj2.GetArea()<<endl;
return 0;
}
第2题
#include<iostream>
using namespace std;
class Student
{
public:
Student(int a,string b);
void display();
~Student();
private:
int no;
string name;
};
Student::Student(int a,string b)
{
no=a;
name=b;
cout<<"执行Student类构造函数"<<endl;
}
void Student::display()
{
cout<<"学号:"<<no<<endl;
cout<<"姓名:"<<name<<endl;
}
Student::~Student()
{
cout<<"执行Student类析构函数"<<endl;
}
int main()
{
Student stud1(2017001,"John");
stud1.display();
return 0;
}
第3题
#include<iostream>
using namespace std;
class Account
{
public:
Account(char name[],long num,float amount); //类的有参构造函数
Account(); //类的无参构造函数
void deposit(float amount); //往账户中存款
int withdraw(float amount); //从账户中取款(考虑透支情况)
float getBalance(); //查询余额
private:
char mName[20]; //银行账户的户名
long mSN; //本账户的帐号
float mBalance; //本账户当前的余额
};
Account::Account(char name[],long num,float amount)//创建账户
{
for(int i=0;i<20;i++)
mName[i]=name[i];
mSN=num;
mBalance=amount;
}
void Account::deposit(float amount)
{
mBalance+=amount;
cout<<"存款成功"<<endl;
}
int Account::withdraw(float amount)
{
if((mBalance-amount)<0) //考虑透支情况
cout<<"余额不足,请重新输入"<<endl;
else
{
mBalance-=amount;
cout<<"取款成功"<<endl;
}
}
float Account::getBalance()
{
cout<<"账户余额为"<<mBalance<<endl;
cout<<"查询成功"<<endl;
}
int main()
{
int a;
char name[20];
float b,mont;
long msn;
cout<<"请输入账户的户名,账号和存款数目"<<endl;
cin>>name>>msn>>mont;
Account account(name,msn,mont);
cout<<"开户成功"<<endl;
while(1)
{
cout<<"请选择服务"<<endl;
cout<<"1.存款"<<endl<<"2.取款"<<endl;
cout<<"3.查询余额"<<endl<<"4.结束服务"<<endl;
cin>>a;
if(a==1)
{
cout<<"请输入存款金额:";
cin>>b;
account.deposit(b);
}
if(a==2)
{
cout<<"请输入取款金额:";
cin>>b;
account.withdraw(b);
}
if(a==3)
account.getBalance();
if(a==4)
break;
}
return 0;
}
第4题
#include<iostream>
using namespace std;
class Stock
{
public:
Stock(string stockname,int stockcode,int stocknum,float stockprice);//获得股票
void addstock(int stocknum);//增持股票
void sellstock(int stocknum);//卖出股票
void update(float stockprice);//更新股票价格
void show(); //显示股票情况
private:
string name;
int code,num;
float price,sum;
};
Stock::Stock(string stockname,int stockcode,int stocknum,float stockprice)
{
name=stockname;
code=stockcode;
num=stocknum;
price=stockprice;
sum=num*price;
}
void Stock::addstock(int stocknum)
{
num+=stocknum;
sum=num*price;
cout<<"增持成功"<<endl;
}
void Stock::sellstock(int stocknum)
{
if(num-stocknum<0)
cout<<"股票数目不足,请重新输入"<<endl;
else
{
num-=stocknum;
sum=num*price;
cout<<"股票卖出成功"<<endl;
}
}
void Stock::update(float stockprice)
{
price=stockprice;
sum=num*price;
cout<<"股票价格已更新"<<endl;
}
void Stock::show()
{
cout<<"股票名称:"<<name<<endl;
cout<<"股票代码:"<<code<<endl;
cout<<"股票数目:"<<num<<endl;
cout<<"每股价格:"<<price<<endl;
cout<<"股票总值:"<<sum<<endl;
}
int main()
{
int a,b,num,code;
string name;
float price,price1;
cout<<"请输入股票名称,股票代码,股票数目和每股价格"<<endl;
cin>>name>>code>>num>>price;
Stock stock(name,code,num,price);
cout<<"获得股票成功"<<endl;
while(1)
{
cout<<"请选择服务"<<endl;
cout<<"1.增持股票"<<endl<<"2.卖出股票"<<endl;
cout<<"3.更新股票价格"<<endl<<"4.显示股票信息"<<endl;
cout<<"5.结束"<<endl;
cin>>a;
if(a==1)
{
cout<<"请输入购买股票数目:";
cin>>b;
stock.addstock(b);
}
if(a==2)
{
cout<<"请输入卖出股票数目:";
cin>>b;
stock.sellstock(b);
}
if(a==3)
{
cout<<"请输入新的股票价格:";
cin>>price1;
stock.update(price1);
}
if(a==4)
stock.show();
if(a==5)
break;
}
return 0;
}
第5题
#include<iostream>
using namespace std;
class Cylinder
{
public:
Cylinder(float radius,float height);
void printVolume(); //显示圆柱体体积
private:
float r,h,v;
};
Cylinder::Cylinder(float radius,float height)
{
r=radius;
h=height;
}
void Cylinder::printVolume()
{
v=3.14*r*r*h;
cout<<"圆柱体体积为:"<<v;
}
int main()
{
float a,b;
cout<<"输入圆柱体的半径和高"<<endl;
cin>>a>>b;
Cylinder cylinder(a,b);
cylinder.printVolume();
return 0;
}
第6题
#include<iostream>
using namespace std;
class Book
{
public:
Book(string name,float bookprice,int num);
void display(); //显示图书情况
void borrow(); //借书并显示图书数量
void restore(); //存书并显示图书数量
private:
string bookname;
float price;
int number;
};
Book::Book(string name,float bookprice,int num)
{
bookname=name;
price=bookprice;
number=num;
}
void Book::display()
{
cout<<"图书名为:"<<bookname<<endl;
cout<<"图书价格:"<<price<<endl;
cout<<"存书数量:"<<number<<endl;
}
void Book::borrow()
{
number-=1;
cout<<"存书数量:"<<number<<endl;
}
void Book::restore()
{
number+=1;
cout<<"存书数量:"<<number<<endl;
}
int main()
{
Book b1("English",20,3), b2("math",24,5);
b1.display();
b1.borrow();
b2.display();
b2.restore();
return 0;
}
第7题
#include<iostream>
#include<cstring>
using namespace std;
class MyString
{
public:
MyString(); //无参构造函数
MyString(const char *s); //构造函数
MyString(const MyString &s); //拷贝构造函数
void MyStrcat(MyString &s); //字符串连接
void display(); //显示字符串
private:
char *str;
};
MyString::MyString() //无参构造函数
{
this->str=new char[10];
str[0]='\0';
}
MyString::MyString(const char *s) //构造函数
{
str=new char[strlen(s)+1];
strcpy(str,s);
}
MyString::MyString(const MyString&s) //拷贝构造函数
{
str=new char[strlen(s.str)+1];
strcpy(str,s.str);
}
void MyString::MyStrcat(MyString &s) //字符串连接
{
int i,j;
char *s1=new char[strlen(str)+1];
strcpy(s1,str);
str=new char[strlen(str)+strlen(s.str)+1];
for(i=0;i<strlen(s1);i++)
str[i]=s1[i];
for(j=0;j<strlen(s.str);j++)
str[i++]=(s.str)[j];
str[i]='\0';
delete[]s1;
}
void MyString::display()
{
cout<<str<<endl;
}
int main()
{
MyString str1("Hello");
MyString str2(" World!");
MyString str3(str1);
MyString str4;
str1.display();
str2.display();
str3.display();
cout<<endl;
str1.MyStrcat(str2);
str2.MyStrcat(str1);
str4.MyStrcat(str1);
str4.MyStrcat(str2);
str1.display();
str2.display();
str3.display();
str4.display();
cout<<endl;
return 0;
}
该博客围绕C++类与对象展开实验。实验内容包括编写Circle、Student等多个类,实现相应功能,如计算圆面积、显示学生信息等。实验结果考察类与对象知识,还给出部分题目的实现方法。最后展示了各题的源代码。
2772

被折叠的 条评论
为什么被折叠?



