book.cpp
//设计BOOK类,包括ISBN号(string),书名(string),作者(string)
//以及单价信息,还能根据数量确定折扣率并计算应付款项,
//折扣率五本以上90%,十本以上 80%,二十本以上70%。
//提示:只设计一个种类的书,
//设计适当的构造及析构函数(类声明和成员函数定义分离)。
using namespace std;
class book
{
private:
string ISBN; //书号
string book_name; //书名
string author; //作者
float uint_price; //单价
public:
book():ISBN("10-120"),book_name("c++ primer"),author("Tom"),uint_price(0){}
book(string n,string str1,string str2,int p)
:ISBN(n),book_name(str1),author(str2),uint_price(p){}
float Discount(int num) const;
};
float book::Discount(int num)const{
float price=uint_price;
if(num>0&&num<5){
return price*num;
}
else if(num>=5&&num<10){
return price*0.9*num;
}
else if(num>=10&&num<20){
return price*num*0.8;
}
else{
return price*num*0.7;
}
}
mystring.cpp
#include <iostream>
#include <cstring>
class MyString{
public:
MyString(char *pstr);
MyString(const MyString& str);
~MyString();
public:
void ShowString(); //显示字符串
void Add(const MyString& str); //与另外一个相加
void Copy(const MyString& str); //从另外一个拷贝
int GetLength();
private:
char *m_buf;
};
MyString::MyString(char *pstr=" "){
int len=strlen(pstr);
m_buf = new char[len];
strcpy(m_buf,pstr);
}
MyString::MyString(const MyString& str){
int len=strlen(str.m_buf);
m_buf = new char(len);
strcpy(m_buf,str.m_buf);
}
void MyString::ShowString(){
cout<<m_buf<<endl;
}
void MyString::Add(const MyString& str){
strcat(m_buf,str.m_buf);
cout<<m_buf<<endl;
}
void MyString::Copy(const MyString& str){
strcpy(m_buf,str.m_buf);
cout<<m_buf<<endl;
}
int MyString::GetLength(){
int len=strlen(m_buf);
return len;
}
MyString::~MyString(){
if(m_buf!=NULL){
delete[] m_buf;
m_buf=NULL;
}
}
Rectangle.cpp
#include <iostream>
//写一个Rectangle类,抽象自己的数据和成员函数.
// 要求:(1) 写出构造函数和析构函数
// (2) 用一个静态数据成员记录所创建的矩形的个数
// (3) 写出GetArea()获得矩形的面积
// (4) 明确哪些函数需要被设计为const成员函数
// (5) 写出一个全局函数获得N个矩形的面积
// int GetAllRectArea( Rectangle* pRectArray, int count );
using namespace std;
class Rectangle{
private:
float length; //长
float width; //宽
static int size; //矩形个数
public:
Rectangle():length(0),width(0){}
Rectangle(float L,float W):length(L),width(W){
size++;
}
float GetArea() const;
int Getsize();
friend int GetAllRectArea(Rectangle*pRectArray,int count);
};
int Rectangle::size=0;
float Rectangle::GetArea()const{
float Area=length*width;
cout<<Area<<endl;
return Area;
}
int Rectangle::Getsize(){
cout<<size<<endl;
return size;
}
int GetAllRectArea(Rectangle *pRectArray,int count){
if(pRectArray==NULL){
return -1;
}
for(int i=0;i<count;i++){
pRectArray->GetArea();
pRectArray++;
}
return 0;
}
student.cpp
#include <iostream>
//扩展练习4(学生类设计)
//设计一个友元函数,按照成绩从高到低的顺序输出姓名,学号和成绩信息.
using namespace std;
class Student{
friend void sort(Student stu[]);
private:
string name;
int id;
float score;
public:
Student():name("tom"),id(18),score(100){}
Student(string str,int a,float s):name(str),id(a),score(s){}
void show_student();
};
void Student::show_student(){
cout<<name<<' '
<<id<<' '
<<score<<' '
<<endl;
}
void sort(Student stu[]){
Student temp;
for(int i=0;i<5;i++){
for(int j=0;j<5-i-1;j++){
if(stu[j].score>stu[j+1].score){
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
}
for(int i=0;i<5;i++){
stu[i].show_student();
}
}
main.cpp
#include <iostream>
#include "student.cpp"
#include "book.cpp"
#include "Rectangle.cpp"
#include "mystring.cpp"
using namespace std;
int main()
{
Student stu1("Tom1",19,10);
Student stu2("Tom2",18,40);
Student stu3("Tom3",17,30);
Student stu4("Tom4",18,20);
Student stu5("Tom5",20,50);
Student arr[10]={stu1,stu2,stu3,stu4,stu5};
sort(arr);
book b1("10-120","c++ primer","Tom",100);
float cost=0;
cost= b1.Discount(5);
cout << cost << endl;
Rectangle R1(10,10);
Rectangle R2(10,20);
Rectangle R3(10,30);
Rectangle R4(10,40);
R1.GetArea();
int count=R1.Getsize();
Rectangle pRectArray[5]={R1,R2,R3,R4};
GetAllRectArea(pRectArray,count);
MyString str1("name");
MyString str(str1);
str.ShowString();
str.Copy("cat");
str.Add("app");
cout << str.GetLength() << endl;
cout << "Hello World!" << endl;
return 0;
}
网友评论