26_类

面向对象编程

  1. 面向对象编程思想,经验的提炼,找出会变化的代码
  2. 抽象:例如所有车都有的功能,根据现实中的经验去分类,可以无限扩展,结构体应该写什么数据,有什么函数
  3. 封装:限制对象的活动范围,get、set,通过函数来中转。能想到类的所有操作方式
    1. 为了保护封装性,C++类给出了权限划分
    2. 为了调用方便,不用每次调用函数

  1. 类的权限划分,public公有,private私有,protected受保护的
  2. 类里面分为两个东西
    1. 属性、数据成员、字段—》都是一个意思
    2. 函数成员、成员函数、方法—》都是一个意思
  3. this表示使用传递进来的指针,如果不同名可以省略this
  4. 栈空间实例化的类对象使用.访问数据成员和成员函数,指针使用->调用。和结构体一样
  5. 成员函数自动传递对象指针
  6. 类与结构体的区别,结构体权限修饰符是默认公有的,类是默认私有的
  7. 类内部算法函数与这个类的功能是无关的,不通用的,使用私有权限修饰
  8. 共用体在C++中也是类,和结构体与类的区别是它的数据成员是共享内存大小的

杂项

  1. 内联函数实现和定义写一起

作业

1. 定义一个点类, 其属性包括点的坐标,提供计算两点之间距离的方法

 //Pointer.h
#pragma once
class Pointer
{
public:
    int getDistance(Pointer *pPointer) const;
    void setX(int x);
    void setY(int y);
    int getX() const;
    int getY() const;
private:
    int m_iX;
    int m_iY;
};
 //Pointer.cpp
#include "Pointer.h"
#include 

int Pointer::getDistance(Pointer *pPointer) const
{
    return (int)sqrt
    (
        pow(getX() - pPointer->getX(), 2) 
            + 
        pow(getY() - pPointer->getY(), 2)
    );
}

void Pointer::setX(int x)
{
    m_iX = x;
}

void Pointer::setY(int y)
{
    m_iY = y;
}

int Pointer::getX() const
{
    return m_iX;
}

int Pointer::getY()const
{
    return m_iY;
}

2. 定义一个矩形类, 其属性包括点的长度和宽度,提供计算矩形面积的方法

 //Rectangle.h
#pragma once
class Rectangle
{
public:
    double getArea() const;
    void setLength(double length);
    void setWidth(double width);
    double getLength() const;
    double getWidth() const;
private:
    double m_dblLength;
    double m_dblWidth;
};
 //Rectangle.cpp
#include "Rectangle.h"

double Rectangle::getArea() const
{
    return getLength() * getWidth();
}

void Rectangle::setLength(double length)
{
    m_dblLength = length;
}

void Rectangle::setWidth(double width)
{
    m_dblWidth = width;
}
double Rectangle::getLength() const
{
    return m_dblLength;
}

double Rectangle::getWidth() const
{
    return m_dblWidth;
}

3. 定义一个三角形类,其属性包括边长,提供计算三角形面积的方法

 //Triangle.h
#pragma once
class Triangle
{
public:
    const double getArea();
    const double getSideOne();
    const double getSideTwo();
    const double getSideThree();
    void setSideOne(double side);
    void setSideTwo(double side);
    void setSideThree(double side);
private:
    double m_dblSideOne;
    double m_dblSideTwo;
    double m_dblSideThree;

};
 //Triangle.cpp
#include "Triangle.h"
#include 

const double Triangle::getArea()
{
    double dblP = (getSideOne() + getSideTwo() + getSideThree()) / 2;
    return sqrt
    (
        dblP * (dblP - getSideOne()) * (dblP - getSideTwo()) * (dblP - getSideThree())
    );
}

const double Triangle::getSideOne()
{
    return m_dblSideOne;
}

const double Triangle::getSideTwo()
{
    return m_dblSideTwo;
}

const double Triangle::getSideThree()
{
    return m_dblSideThree;
}

void Triangle::setSideOne(double side)
{
    m_dblSideOne = side;
}

void Triangle::setSideTwo(double side)
{
    m_dblSideTwo = side;
}

void Triangle::setSideThree(double side)
{
    m_dblSideThree = side;
}

4. 定义一个圆形类, 其属性包括圆心和半径,建两个圆形对象

 //Circular.h
#pragma once
class Circular
{
public:
    double getCenter() const;
    double getRadius() const;
    void setCenter(double center);
    void setRadius(double radius);
private:
    double m_dblCenter;
    double m_dblRadius;
};
 //Customer.cpp
#include "Customer.h"
#include 

using namespace std;

const char * Customer::getName() const
{
    return m_pName;
}

const char * Customer::getId() const
{
    return m_pId;
}

int Customer::getDeposit() const
{
    return m_iDeposit;
}

void Customer::setName(char *name)
{
    m_pName = name;
}

void Customer::setId(char *id)
{
    m_pId = id;
}

void Customer::setDeposit(int deposit)
{
    m_iDeposit = deposit;
}

void Customer::storeDeposit(char *name, char *id, int deposit)
{
    if (strstr(getName(), name) == 0
        &&
        strstr(getId(), id) == 0
        )
    {
        m_iDeposit = m_iDeposit + deposit;
    }
}

void Customer::fetchDeposit(char *name, char *id, int deposit)
{
    if (strstr(getName(), name) == 0
        &&
        strstr(getId(), id) == 0
        )
    {
        m_iDeposit = m_iDeposit - deposit;
    }
}

5. 定义一个日期类, 其属性包括年、月、日,提供求闰年、星期的方法

 //MyDate.h
class MyDate
{
public:
    void setYear(unsigned year);
    void setMonth(unsigned month);
    void setDay(unsigned day);
    unsigned getYear() const;
    unsigned getMonth() const;
    unsigned getDay() const;
    bool isLeapYear() const;
    unsigned getDayOfWeek() const;
private:
    unsigned m_uYear;
    unsigned m_uMonth;
    unsigned m_uDay;
};
 //MyDate.cpp
#include "MyDate.h"

void MyDate::setYear(unsigned year)
{
    m_uYear = year;
}

void MyDate::setMonth(unsigned month)
{
    m_uMonth = month;
}

void MyDate::setDay(unsigned day)
{
    m_uDay = day;
}

unsigned MyDate::getYear() const
{
    return m_uYear;
}

unsigned MyDate::getMonth() const
{
    return m_uMonth;
}

unsigned MyDate::getDay() const
{
    return m_uDay;
}

bool MyDate::isLeapYear() const
{
    if (getYear() % 400 == 0)
    {
        return true;
    }
    else if (getYear() % 4 == 0 && getYear() % 100 != 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

unsigned MyDate::getDayOfWeek() const
{
    if (getDay() >= 1 && getDay() <= 5)
    {
        return 1;
    }
    else if (getDay() >= 6 && getDay() <= 10)
    {
        return 2;
    }
    else if (getDay() >= 11 && getDay() <= 15)
    {
        return 3;
    }
    else if (getDay() >= 16 && getDay() <= 20)
    {
        return 4;
    }
    else if (getDay() >= 21 && getDay() <= 25)
    {
        return 5;
    }
    else if (getDay() >= 26 && getDay() <= 30)
    {
        return 6;
    }
    else if (getDay() == 31)
    {
        return 7;
    }
    else
    {
        return -1;
    }
}

6. 定义一个时间类, 其属性包括时、分、秒,提供求总秒数的方法

 //Time.h
#pragma once
class Time
{
public:
    void setHour(unsigned hour);
    void setMinute(unsigned minute);
    void setSecond(unsigned second);
    unsigned getHour() const;
    unsigned getMinute() const;
    unsigned getSecond() const;
    unsigned getSumSecond() const;
private:
    unsigned m_uHour;
    unsigned m_uMinute;
    unsigned m_uSecond;
};
 //Time.cpp
#include "Time.h"

void Time::setHour(unsigned hour)
{
    m_uHour = hour;
}

void Time::setMinute(unsigned minute)
{
    m_uMinute = minute;
}

void Time::setSecond(unsigned second)
{
    m_uSecond = second;
}

unsigned Time::getHour() const
{
    return m_uHour;
}

unsigned Time::getMinute() const
{
    return m_uMinute;
}

unsigned Time::getSecond() const
{
    return m_uSecond;
}

unsigned Time::getSumSecond() const
{
    return getHour() * 60 + getMinute() * 60 + getSecond();
}

7. 定义一个储户类, 其属性包括储户姓名、账号、存款,提供存入指定的存款、取出指定的存款

 //Customer.h
class Customer
{
public:
    const char * getName() const;
    const char * getId() const;
    int getDeposit() const;
    void setName(char *name);
    void setId(char *id);
    void setDeposit(int deposit);
    void storeDeposit(char *name, char *id, int deposit);
    void fetchDeposit(char *name, char *id, int deposit);
private:
    char * m_pName;
    char * m_pId;
    int m_iDeposit;
};
 //Customer.cpp
#include "Customer.h"
#include 

using namespace std;

const char * Customer::getName() const
{
    return m_pName;
}

const char * Customer::getId() const
{
    return m_pId;
}

int Customer::getDeposit() const
{
    return m_iDeposit;
}

void Customer::setName(char *name)
{
    m_pName = name;
}

void Customer::setId(char *id)
{
    m_pId = id;
}

void Customer::setDeposit(int deposit)
{
    m_iDeposit = deposit;
}

void Customer::storeDeposit(char *name, char *id, int deposit)
{
    if (strstr(getName(), name) == 0
        &&
        strstr(getId(), id) == 0
        )
    {
        m_iDeposit = m_iDeposit + deposit;
    }
}

void Customer::fetchDeposit(char *name, char *id, int deposit)
{
    if (strstr(getName(), name) == 0
        &&
        strstr(getId(), id) == 0
        )
    {
        m_iDeposit = m_iDeposit - deposit;
    }
}

8. 定义一个股票类, 其属性包括公司名称、持有股票数量、每股价格、总价格,提供增加持有股票、减少持有股票方法

 //Stock.h
#pragma once
class Stock
{
public:
    void setCompanyName(char *name);
    void setNumOfShares(unsigned num);
    void setPriceOfShars(unsigned price);
    char *getCompanyName() const;
    unsigned getNumOFShares() const;
    unsigned getPriceOfShares() const;
    unsigned getTotalPrice() const;
    void addNumOfShares(unsigned num);
    void reduceNumOfShares(unsigned num);
private:
    char *m_pCompanyName;
    unsigned m_uNumOfShares;
    unsigned m_uPriceOfShars;
    unsigned m_uTotalPrice;
};
 //Stock.cpp
#include "Stock.h"

void Stock::setCompanyName(char *name)
{
    m_pCompanyName = name;
}

void Stock::setNumOfShares(unsigned num)
{
    m_uNumOfShares = num;
}

void Stock::setPriceOfShars(unsigned price)
{
    m_uPriceOfShars = price;
}

char * Stock::getCompanyName() const
{
    return m_pCompanyName;
}

unsigned Stock::getNumOFShares() const
{
    return m_uNumOfShares;
}

unsigned Stock::getPriceOfShares() const
{
    return m_uPriceOfShars;
}

unsigned Stock::getTotalPrice() const
{
    return getNumOFShares() * getPriceOfShares();
}

void Stock::addNumOfShares(unsigned num)
{
    m_uNumOfShares = m_uNumOfShares + num;
}

void Stock::reduceNumOfShares(unsigned num)\
{
    m_uNumOfShares = m_uNumOfShares - num;
}

9. 定义一个书类, 其属性包括书名、价格、出版日期,提供出版年限的方法,需要重用前面习题的类

 //Book.h
#include "Book.h"

void Book::setBookName(char *name)
{
    m_pBookName = name;
}

void Book::setBookPrice(unsigned price)
{
    m_uPrice = price;
}

char *Book::getBookName() const
{
    return m_pBookName;
}

unsigned Book::getBookPrice() const
{
    return m_uPrice;
}

unsigned Book::getBookYear() const
{
    return m_date.getYear();
}
 //Book.cpp
#include "Book.h"

void Book::setBookName(char *name)
{
    m_pBookName = name;
}

void Book::setBookPrice(unsigned price)
{
    m_uPrice = price;
}

char *Book::getBookName() const
{
    return m_pBookName;
}

unsigned Book::getBookPrice() const
{
    return m_uPrice;
}

unsigned Book::getBookYear() const
{
    return m_date.getYear();
}

10.定义一个学生类, 其属性包括姓名、年龄,出生年月,提供计算年龄的方法,需要重用前面习题的类

 //Student.h
#pragma once
#include "MyDate.h"

class Student
{
public:
    void setName(char *name);
    char * getName() const;
    unsigned getAge() const;
private:
    MyDate m_date;
    char * m_pName;
    unsigned m_uAge;
};
 //Student.cpp
#include "Student.h"

void Student::setName(char *name)
{
    m_pName = name;
}

char * Student::getName() const
{
    return m_pName;
}

unsigned Student::getAge() const
{
    return 2019 - m_date.getYear();
}
0 条评论
发表一条评论