美文网首页
类与对象

类与对象

作者: 1墨家巨子 | 来源:发表于2019-10-09 11:03 被阅读0次

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;
}

相关文章

  • 类与对象(类与对象定义)

    类与对象的定义和使用 如果在程序之中要定义一个类可以使用class关键字完成,而定义的语法如下: 在这个类中只是定...

  • 函数类和对象区别

    类与类:行为不同 对象与对象:数据不同

  • python入门开发学习笔记之类与对象

    本节重点 掌握什么是类、什么是对象 掌握如何定义及使用类与对象 了解对类与对象之间的关系 类与对象的概念 类即类别...

  • 类对象与类的对象

    类对象(obj.getClass)描述的是类的代码信息,比如哪些属性、属性是什么类型、变量名是什么、哪些方法、方法...

  • python-高级、面向对象

    一、类与对象 二、类和对象

  • Java面向对象笔记

    类和对象 对象的概念 什么是面向对象 类 什么是对象的属性 什么是对象的方法 类与对象的关系/与区别 什么是对象 ...

  • 006-面向对象1

    面向过程与面向对象 开车问题 吃饭问题 做饭问题 类与对象 类的概念 对象的概念 练习: 区分类与对象 类的组成 ...

  • 三、元类对象的本质

    1. 元类对象结构。 与类对象的结构一样。 与类对象比较 2. 元类对象的获取。 通过object_getClas...

  • 格物致知iOS类与对象

    格物致知iOS类与对象 格物致知iOS类与对象

  • 类与对象

    类与对象是整个面向对象之中最为基础的组成单元;类是共性的集合,对象是个性的产物。所有的类都是用来描述出对象的结构,...

网友评论

      本文标题:类与对象

      本文链接:https://www.haomeiwen.com/subject/yjbwpctx.html