构造函数,析构函数,this指针,对象数组:
#pragma once
// class.h
#ifndef _CLASS_H_
#define _CLASS_H_
#include<string>
using namespace std;
class Stock
{
private:
string company;
int shares;
double share_val;
double total_val;
void set_tot()
{
total_val = shares * share_val;
}
public:
Stock(); //默认构造函数,也可以利用全参数默认,即Stock(const std::string &name = NAME, int sh =0,double sh_v=0)
Stock(const string& name, int sh=0, double sh_v=0);
~Stock();//析构函数
void buy(long num, double price);
void sell(long num, double price);
void update(double price);
void show()const;//此const可以确保该函数访问的隐性数据不发生改变
const Stock& topval(const Stock& s) const;
//上面的topval中含有三个const
// 第一个const是使得返回值的类型为const的Stock类的引用对象
// 第二个const是保证引用的Stock对象不发生改变
// 第三个const是确保此函数访问的隐性数据不发生改变
};
#endif
// fuc.cpp
#include<iostream>
#include"class.h"
Stock::Stock() //默认构造函数
{
company = "none";
shares = 0;
share_val = 0;
set_tot();
}
Stock::Stock(const string& name, int sh, double sh_v) //默认参数只需要写在声明中,而在函数定义中不能加上默认参数
{
company = name;
if (sh < 0)
{
cout << "shares can not under 0" << endl;
cout << "You have no shares in " << name << endl;
}
else
{
shares = sh;
share_val = sh_v;
set_tot();
}
}
Stock::~Stock()
{
cout << "the end of the object" << endl;
}
void Stock::buy(long num, double price)
{
if (num < 0)
{
cout << "The number is negative" << endl;
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(long num, double price)
{
if (num < 0)
{
cout << "The number is negative" << endl;
}
else
{
shares -= num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
void Stock::show()const
{
cout << "Company: " << company << endl;
cout << "Shares: " << shares << endl;
cout << "Share_value: " << share_val << endl;
cout << "Total_value: " << total_val << endl;
}
const Stock& Stock::topval(const Stock& s) const // const Stock&为返回类型,而Stock::加在函数名前
{
if (s.total_val > total_val)
{
return s;
}
else if (s.total_val < this->total_val) //this是指向调用对象的指针: 所以 this->total_val 等价于 total_val
{
return *this; // this是指针,则*this就相当于该调用对象的一个引用
}
}
//构造函数: 与类名相同 eg: 类名Stock 则 构造函数名为Stock
// 同时,若程序员没有自己设置构造函数,则系统会自动设置默认构造函数,且不进行初始化
// 若程序员自己设置了构造函数,则必须同时自己设置一个默认构造函数,可以通过默认参数或者函数重载来设置默认构造函数
// 初始化的显式方法和隐式方法: 显式: Stock stock1 = Stock( , , ,) 隐式: Stock stock2( , , ,) -- 初始化过程相当于调用函数过程
//析构函数: 在类名前加上一个~ eg: 类名Stock 则 析构函数名为~Stock
// 析构函数在类的对象内存被回收时,发挥作用
// this指针:指针指向调用对象 eg: Stock stock 则 stock.show() (直接访问) 与 this -> show() (通过地址间接访问)的语义相同
// this是一个指针,则 *this 则相当于调用对象的一个引用
// 对象数组: 与结构体数组类似
// eg: Stock stock[] -- 初始化时要通过显式方法: Stock{ , , ,}
// main.cpp
#include<iostream>
#include"class.h"
int main()
{
//创建对象数组,并用自己创建的构造函数来进行初始化
Stock stocks[4] =
{
Stock("GodFishhh",100,200),
Stock("AFish",200,80),
Stock("Boffo",20,50),
Stock("Fleep",30,60),
};
cout << "Shares holding: " << endl;
int i;
for (i = 0; i < 4; i++)
{
stocks[i].show();
cout << "---------------------" << endl;
}
//找出total_val最高的对象
const Stock* top = &stocks[0]; //将top设为对象数组的首地址,以便后续进行比较
for (i = 1; i < 4; i++)
{
top = &top->topval(stocks[i]);
//逻辑: top是一个对象的地址,所以通过top->访问topval函数,并传入参数stocks[i],同时函数的返回值为Stock对象引用,所以要取其地址再赋值给top
}
cout << "The top value information: " << endl;
top->show();
system("pause");
return 0;
}
//1.构造函数: 与类名相同 eg: 类名Stock 则 构造函数名为Stock
// 同时,若程序员没有自己设置构造函数,则系统会自动设置默认构造函数,且不进行初始化
// 若程序员自己设置了构造函数,则必须同时自己设置一个默认构造函数,可以通过默认参数或者函数重载来设置默认构造函数
// 初始化的显式方法和隐式方法: 显式: Stock stock1 = Stock( , , ,) 隐式: Stock stock2( , , ,) -- 初始化过程相当于调用函数过程
//2.析构函数: 在类名前加上一个~ eg: 类名Stock 则 析构函数名为~Stock
// 析构函数在类的对象内存被回收时,发挥作用
// 3.this指针:指针指向调用对象 eg: Stock stock 则 stock.show() (直接访问) 与 this -> show() (通过地址间接访问)的语义相同
// this是一个指针,则 *this 则相当于调用对象的一个引用
// 4.对象数组: 与结构体数组类似
// eg: Stock stock[] -- 初始化时要通过显式方法:
Stock stock[2] = { Stock("GodFishhh",100,200), Stock("AFish" , 100 ,300), } ;