类定义使用 (初步)
/////////////inline内联函数///////////////////////////
//*类里面定义的为内联
//*外定义需加inline使其内联
//////////////构造函数////////////////////////////////
//*与类同名 且没有返回类型
//*使用:
1> Stock gar=Stock("hello",10,20.2);//会产生临时对象,再复制到gar中,如此需调用析构函数
// 也可能不产生,用 "2> " 得方法
2> Stock gar("hello",10,20.2);
3> Stcok *pp=new Stock("hello",10,20.2);
//*构造函数是用来构造对象的,所以不能被对象调用
//*用构造函数赋值 gar=Stock("new val",20,30.2);// 肯定在赋值前创建个临时对象
//////////析构函数////////////////////////////////////
//*创建对象时跟踪,对象消亡时自动调用析构函数
//*没返回类型 ,无参数
//*类名前加 ~ 符号
/////////////类赋值////////////////////////////////////////
//*可以将对象赋给同类型另一对象
/////////////////const成员函数/////////////////
//*void Stock:show() const //方法不修改调用对象,应申明const
////////////////////this指针////////////////////////
//*指向创建的对象,存储对象的地址
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
////////////////////////////////////////////////////////////////////////////////////////////////
// 例子分3个文件 stock.h,main.cpp,stock.cpp
////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////stock.h//////////////////////////////
#ifndef stock_H_
#define stock_H_
#include <iostream>
#include <cstring>
class Stock
{
private: //private:(default) public: protected:
char company[30];
int shares;
double share_val;
double total_val;
void set_tot(){total_val=shares*share_val;};
public:
Stock();
Stock(const char *co,int n=0,double pr=0);
~Stock(); //no parameter
void buy(int num,double price);
void sell(int num,double price);
void updata(double price);
void show() const;
const Stock & Stock::topval(const Stock & s) const;
};
#endif
////////////////main.cpp///////////////////////////////
#include <iostream>
#include "stock.h"
const int STKS=4;
int main()
{
using std::cout;
using std::ios_base;
Stock stocks[STKS]=
{
Stock("nansmart",12,20.0),
Stock("boff",200,2.0),
Stock("monona",130,3.25),
Stock("fleera",60,6.5)
};
cout.precision(2);
cout.setf(ios_base::fixed,ios_base::floatfield);
cout.setf(ios_base::showpoint);
cout<<"stock holding:\n";
int st;
for (st=0;st<STKS;st++)
stocks[st].show();
Stock top=stocks[0];
for(st=0;st<STKS;st++)
top=top.topval(stocks[st]);
cout<<"\nMost val holding:\n";
top.show();
return 0;
}
////////////////stock.cpp//////////////////////////////
#include "hello.h"
Stock::Stock()
{
std::cout<<"default"<<std::endl;
strncpy(company,"no name",7);
share_val=0.0;
total_val=0.0;
}
Stock::Stock(const char *co,int n,double pr)
{
strncpy(company,co,29);
company[29]='\0';
if (n<0)
{
std::cerr<<"number of shares can't be negative."
<<company<<" shares set to 0.\n";
shares=0;
}
else
shares=n;
share_val=pr;
set_tot();
}
Stock::~Stock()
{
std::cout<<"i was be used.\n"<<std::endl;
}
void Stock::buy(int num,double price)
{
if (num<0)
{
std::cerr<<"number of shares purchased can't be negative."
<<"transaction is aborted.\n";
}
else
{
shares+=num;
share_val=price;
set_tot();
}
}
void Stock::sell(int num,double price)
{
using std::cerr;
if (num<0)
{
cerr<<"number of shares sold can't be negative."
<<"transaction is aborted.\n";
}
else if(num>shares)
{
cerr<<"you can't do it.\n";
}
else
{
// cerr<<" here: "<<shares<<" over "<<std::endl;
shares-=num;
share_val=price;
set_tot();
}
}
void Stock::updata(double price)
{
share_val=price;
set_tot();
}
void Stock::show() const
{
using std::cout;
using std::endl;
cout<<"company: "<<company
<<" shares: "<<shares<<endl
<<" share price:$ "<<share_val
<<" total worth:$ "<<total_val<<endl;
}
const Stock & Stock::topval(const Stock & s) const
{
if (s.total_val>total_val)
return s;
else
return *this;
}


浙公网安备 33010602011771号