您的位置:首页 > 编程语言 > C语言/C++

c++基础之类的基礎

2009-04-23 09:38 169 查看
类规范一般有二个部分组成:
1.类声明:以数据成员的方式描绘数据部分,以成员函数(被称为方法)去描绘公有接口
2.类方法定义:
描绘如何实现类成员的函数

发开发一个类需要多部,这里开发分为多个阶段而不是一次性完成。
第一个阶段
类声明。
类声明类似结构什么,可以包括数据成员和函数成员。声明和私有成员,
#include <iostream.h>
#include <cstring.h>
class Stock()
{
private:
char company[30];
in share;
double share_val;
double total_val;
void set_tot()
{
total_val=share*share_val;
}
public:
void acquire(const char * co,int n,double pr);
void buy(int num,double price);
void sell(int nuum,double price);
void update(double price);
void show();
};

第二阶段
实现类成员函数
还需要为创建描绘的第二部分:为那些类声明中的原型表示的成员函数提供代码。
成员函数与常规的函数十分相似,他有函数头和函数体。也可以有返回值和参数。但是他还有二个特征:
1.定义成员函数的时候,必须使用域解析符(::)來說明函数属于那个类。
2.类方法可以访问类的private组件
EG:
假設Bozo有個函數名為report()成員函數,該函數返回char指針,則其函數頭如下
char *Bozo::Report
void Stock::acquire(const char *co,int n,double pr)
{
std::strncpy(company,co,29);
company[29]='\0';
if(n<0)
{
std::cerr<<"Number of share can't be negative"<<company<<"share set to 0\n";
share=0;
}
else
share=n;
share_val=pr;
set_tot();
}
void Stock::buy(int num,double price)
{
if(num<0)
{
std::cer<<"Number of share can't purchared can't be begative"
<<"Transaction is aborted.\n";
}
else
{
share+=num;
share_var=price;
set_tot();
}
}
void Stock::sell(int num,double price)
{
using std::cerr;
if(num<0)
{
cerr<<"Number of share sold can't be negative"
<<"Transacton is aborted.\n";
}
else if(num>share)
{
cerr<<"you can't sell more than what you have!"
<<"Transation is aborted.\n";

}
else
{
share-=num;
share_val=price;
set_tot();
}
}
void Stock::update(double price)
{
share_val=price;
set_tot();
}
void Stock::show()
{
using std::cout;
using std::endl;
cout<<"company"<<company<<"share:"<<share<<endl
<<"share price$"<<share_val<<"Total Worth$"<<total_val_endl;

第三阶段
我们在前面什么了类和类成员函数
最后我们要使用这些类
C++的目標是使用類,
要创建类对象,可以声明类变量,也可以使用new为类对象分配空间。
可以将对象作为函数的参数和返回值,创业可以将一个对象赋给另外一个。
#include <iostream.h>
#include <cstring.h>
class Stock
{
private:
char company[30];
in share;
double share_val;
double total_val;
void set_tot()
{
total_val=share*share_val;
}
public:
void acquire(const char * co,int n,double pr);
void buy(int num,double price);
void sell(int nuum,double price);
void update(double price);
void show();
};
void Stock::acquire(const char *co,int n,double pr)
{
std::strncpy(company,co,29);
company[29]='\0';
if(n<0)
{
std::cerr<<"Number of share can't be negative"<<company<<"share set to 0\n";
share=0;
}
else
share=n;
share_val=pr;
set_tot();
}
void Stock::buy(int num,double price)
{
if(num<0)
{
std::cer<<"Number of share can't purchared can't be begative"
<<"Transaction is aborted.\n";
}
else
{
share+=num;
share_var=price;
set_tot();
}
}
void Stock::sell(int num,double price)
{
using std::cerr;
if(num<0)
{
cerr<<"Number of share sold can't be negative"
<<"Transacton is aborted.\n";
}
else if(num>share)
{
cerr<<"you can't sell more than what you have!"
<<"Transation is aborted.\n";

}
else
{
share-=num;
share_val=price;
set_tot();
}
}
void Stock::update(double price)
{
share_val=price;
set_tot();
}
void Stock::show()
{
using std::cout;
using std::endl;
cout<<"company"<<company<<"share:"<<share<<endl
<<"share price$"<<share_val<<"Total Worth$"<<total_val_endl;
}
int main()
{

Stock stock1;
stock1.acquire("NanoSmart",20,12.50);
cout.setf(ios_base::fixed);
cout.precision(2);
cout.setf(ios_base::showpoint);
stock1.show();
stock1.buy(15,18.25);
stock1.show();
stock1.sell(400,20.00);
stock1.show();
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  职场 休闲 C++入门