// 第十章 类
注:参考c++ primer plus
一.类
1. 类的声明
class Stock
{
private: //私有,一般都是数据项
std::string company;
int shares;
double share_val;
double total_val;
void set_tot();//私有的成员函数,自动为内联函数
public: //公有,一般为函数
Stock(); //默认构造函数可以为空
Stock(const std::string cname,int n = 0,double pr = 0);//构造函数,一般都带默认参数
~Stock();//析构函数,当构造函数里使用new分配内存时,析构函数调用delete释放。
//这样做就可以不用手动释放内存,c++比c方便的地方
void acquire(const std::string cname,int n,double pr);
void update(double price);
void buy(int num);
void sell(int num);
void show()const;// 只要是不修改变量的函数都要 定义成const函数,不然调用是就会出错
const Stock & Stock::topval(const Stock & s)const;//返回股价高的
//QT 中还有信号
};//定义类的时候一定要检查最后有没有 ";"
Stock 中的私有成员 void set_tot(); 在定义方法时必须加inline,如下
inline void Time::set_tot(){total_val = shares*share_val;}//定义时必须使用 inline
Stock 中其他函数的method
Stock::Stock(const std::string cname,int n,double pr{//构造函数原型定义
....
}
void Stock::acquire(const std::string cname,int n,double pr);{ //Stock类成员函数原型定义
....
}
2. //类的创建
Stock sam("Baiud"); | Stock sam = Stock("Baiud");// 两种方式都可以。省略参数,为默认参数
//若构造函数为 Stock(int a); 以下定义都正确
Stock stock1(1); | Stock sam = Stock(1);
Stock stock2 = 5;//也可以这样,若在类的声明中构造函数前加 explicit ,则会关闭这种写法
//即 explicit Stock(int a);
3. //const 成员函数
void show()const;// 只要是不修改变量的函数最好定义成const函数,以防调用时会出错
//比如下面代码
const Stock tom("tenct",10,10);
tom.show();//此行会报错,由于不确定show是否会改变变量的值。
4. //this 指针
const Stock & Stock::topval(const Stock & s)const{
if(s.total_val > total_val)
return s;
else
return *this;// this 指向调用的对象地址
}
const Stock & top = tom.topval(sam);//tom被隐式调用,sam显示调用。this指向隐式调用
cout << "\n" << "top: " << endl;
top.show();
5. //创建对象数组
Stock stocks[10] = {//必须为每个对象调用构造函数
Stock("Baiud"), //后两个参数为默认参数
Stock("360",10,12.5),
Stock(),//默认构造函数
...//余下7个对象,都将被调用默认构造函数
};
二.函数重载条件
1.函数名相同,在相同的作用域内,其参数类型、参数个数,参数顺序不同等能构成函数重载,
2.同时在类中,对于函数名相同的const函数和非const函数能够构成重载,同时它们被调用的时机为:如果定义的对象是常对象,则调用的是const成员函数,如果定义的对象是非常对象,则调用重载的非const成员函数。
注:返回值类型不同不能被重载
三.本章代码
1.头文件
#ifndef __STOCK_H__
#define __STOCK_H__
#include <string>
class Stock
{
private:
std::string company;
int shares;
double share_val;
double total_val;
void set_tot(){total_val = shares*share_val;}
public:
Stock();
Stock(const std::string cname,int n = 0,double pr = 0);
~Stock();
void acquire(const std::string cname,int n,double pr);
void update(double price);
void buy(int num);
void sell(int num);
void show()const;
const Stock & topval(const Stock & s)const;
};
#endif
2.源文件
#include "stock.h"
#include <iostream>
Stock::Stock(){
company = "no name";
shares = 0;
share_val = 0;
set_tot();
}
Stock::Stock(const std::string cname,int n,double pr){
if(cname.length() <= 0){
std::cerr << "company name is error. \n";
}
company = cname;
shares = n;
share_val = pr;
set_tot();
}
Stock::~Stock(){
std::cout << "\nI'm destructor!..." << "Bybe\n";
}
void Stock::acquire(const std::string cname,int n,double pr){
if(cname.length() <= 0){
std::cerr << "company name is error. \n";
}
else {
company = cname;
if(n < 0){
std::cerr << "Number of shares can't be negative."
<< company << "shars set to 0.\n";
}
else
shares = n;
share_val = pr;
set_tot();
}
}
void Stock::update(double price){
share_val = price;
set_tot();
}
void Stock::buy(int num){
if(num < 0){
std::cerr << "Number of shares purchased can't be negative."
<< "Transaction is aborted.\n";
}
else{
shares += num;
set_tot();
}
}
void Stock::sell(int num){
if(num < 0){
std::cerr << "Number of shares purchased can't be negative."
<< "Transaction is aborted.\n";
}
else{
shares -= num;
set_tot();
}
}
void Stock::show()const{
using std::cout;
using std::endl;
cout << "Company : " << company << endl;
cout << "Shares : " << shares << endl;
cout << "Share price : " << share_val << endl;
cout << "Total worth : " << total_val << endl;
}
const Stock & Stock::topval(const Stock & s)const{
if(s.total_val > total_val)
return s;
else
return *this;
}