类的构造函数和析构函数

<pre name="code" class="cpp">///类的数据成员是私有的,所以不能使用常规的赋值方法,像结构体那种的。。。。。。
///一般来讲。最好在创建对象的同时,对它进行初始化。
///例如: Stock gift;    gift.buy(10, 22.22);   这种情况下,company 没被赋值。
///程序声明对象时, 自动调用构造函数。
///一定不能将类成员名称用作构造函数的参数名。
///构造函数的参数表示的不是类成员,而是赋给类成员的值。
/*
Stock::Stock(const string & co, long n, double pr)
{
    company = co;
    if(n<0)
    {
        cout <<"Number of shares can't be negative;"
            << company << "shares set to 0.\n";
        shares = 0;
    }
    else
        shares = n;
    share_val = pr;
    set_tot();
}
*/
///有两种使用构造函数初始化对象的方式;
/// 显式:Stock garment = Stock("Furry Mason", 50, 2.5);
///隐式:Stock garment("Furry Mason", 50, 2.5);
///每次创建类对象(甚至使用new动态分配内存)时,C++都是用类构造函数。
///如: Stock *pstock = new Stock("SHINee", 18, 19.0);
///一般来讲,使用对象来调用方法: stock1.show();
///无法使用对象来调用构造函数,因为在构造函数构造出对象之前,
///对象是不存在的。

///默认构造函数是在为提供显式初始值时,用来创建对象的
///构造函数。若未提供任何构造函数,C++将自动提供默认构造函数。
///Stock类的默认构造函数可能如下:
///Stock::Stock(){}
///为类定义了构造函数后,就必须为其提供默认构造函数。
///定义默认构造函数的方式有两种:
///一种是给已有构造函数的所有参数提供默认值:
///Stock(const string & co = "Error", int n = 0, double pr = 0.0);
///另一种方式是通过函数重载来定义另一个构造函数————一个没有参数的构造函数。
///Stock();
///用户定义的默认构造函数通常给所有成员提供隐式初始值。
///在设计类时,通常应该提供对所有类成员做隐式初始化的默认构造函数。
/*Stock::Stock()
{
    company = "no name";
    shares = 0;d
    share_val = 0.0;
    total_val = 0.0;
}
*/
///Stock类的析构函数为~Stock();
///构造函数和析构函数都没有返回值和声明类型。
///但是呢, 和构造函数不同,析构函数没有参数。
///默认构造函数无参数(default constructor)

/*
Stock::~Stock()
{

}
*/
/*
Stock stock2 = Stock("Boffo Objects", 2, 2.0);
stock1 = Stock("Nifty Foods", 10, 50.0);
///第一条语句是初始化,它创建有指定值的对象,可能会创建临时对象,
///(也可能不会),第二条语句是赋值,总会导致在赋值前创建一个临时对象。
///通过初始化或赋值的方式来设置对象的值,通常初始化的效率更高。
*/
///C++11列表初始化:
///Stock hot_hip = {"shdcfs", 22, 89.0};
///Stock jock{"hjh"};
///Stock temp{};

///创建jock时,第二第三个参数将为默认值0和0.0.
///第三个声明与默认构造函数匹配
///要注意const的使用,在show那里。

///接受一个参数的构造函数允许使用赋值语法将对象初始化为一个值。
///若创建对象时没进行显式初始化,则需调用默认构造函数。
///默认构造函数没有参数,如果有, 则需提供默认值。
///未被初始化的对象,程序使用默认构造函数来创建。
///每个类都只能有一个析构函数。new    ~~~      delete.








 
///stock10.cpp
#include <iostream>
#include"stock10.h"
using namespace std;

Stock::Stock()
{
    cout <<"Default constructor called\n";
    company = "no name";
    shares = 0;
    share_val = 0.0;
    total_val = 0.0;
}
Stock::Stock(const string & co, long n, double pr)
{
    cout << "Constructor using " << co << " called\n";
    company = co;
     if(n<0)
    {
        cout <<"Number of shares can't be negative;"
            << company << "shares set to 0.\n";
        shares = 0;
    }
    else
        shares = n;
    share_val = pr;
    set_tot();
}
///class destructor
Stock::~Stock()
{
    std::cout <<"Bye, " << company << "!\n";
}
///other methods
void Stock::buy(long num, double price)
{
    if(num<0)
    {
        cout <<"Number of shares purchased can't be negative;"
            << "Transaction is aborted .\n";
    }
    else
    {
        shares += num;
        share_val = price;
        set_tot();
    }
}
void Stock::sell(long num, double price)
{
    if(num<0)
    {
        cout << "Numbers of shares sold can't be negative;"
             << "Transaction is aborted.\n";
    }
    else if (num > shares)
    {
        cout <<"You can't sell more than you have; "
             << "Transaction is aborted.\n";
    }
    else
    {
        shares -= num;
        share_val = price;
        set_tot();
    }
}
void Stock::update(double price)
{
    share_val = price;
    set_tot();
}
/*void Stock::show()
{
    cout <<"Company: " << company
        << " Shares: " << shares << "\n"
        << "Shares Price: $ " << share_val
        << " Total Worth: $ " <<total_val
        << endl;
}*/
void Stock::show()
{
    using std::cout;
    using std::ios_base;
    ///set format to #.###
    ios_base::fmtflags orig =
        cout.setf(ios_base::fixed, ios_base::floatfield);
    std::streamsize prec = cout.precision(3);
    cout <<"Company: " << company
        << " Shares: " << shares << "\n"
        << "Shares Price: $ " << share_val;
    ///set format to #.##
    cout.precision(2);
    cout << " Total Worth: $ " <<total_val<< endl;
    ///restore original format
    cout.setf(orig, ios_base::floatfield);
    cout.precision(prec);
}
///stock10.h
#ifndef STOCK10_H_INCLUDED
#define STOCK10_H_INCLUDED

#include<string>
class Stock
{
private:
    std::string company;
    long shares;
    double share_val;
    double total_val;
    void set_tot()
    {
        total_val = shares * share_val;
    }
public:
    Stock();
    Stock(const std::string & co, long n = 0,double pr = 0.0);
    ~Stock();
    void buy(long num, double price);
    void sell(long num, double price);
    void update(double price);
    void show();

};

#endif // STOCK10_H_INCLUDED
<pre name="code" class="cpp">///usestock10.cpp
#include<iostream>
#include"stock10.h"
using namespace std;
int main()
{
    {
        cout << "Using constructors to create new objects\n";
        Stock stock1("NanoSmart", 12, 20.0);
        stock1.show();
        Stock stock2 = Stock("Boffo Objects", 2, 2.0);
        stock2.show();

        cout << "assigning stock1 to stock2:\n";
        stock2 = stock1;
        cout << "Listening stock1 and stock2:\n";
        stock1.show();
        stock2.show();

        cout << "Using a constructor to reset an object\n";
        stock1 = Stock("Nifty foods", 10, 50.0);
        Stock stock3 = Stock("hhjh", 1, 1);/// 这种只用到了构造函数
        stock3 = Stock("aaa", 1, 1);///这种不仅用到了构造函数 还用到了析构函数
        cout << "Revised stock1:\n";
        stock1.show();
        cout << "Done\n";
        ///最后还要走一遍析构函数
    }
    return 0;
}
///好吧 我承认我的注释都是最通俗的语言~~~
/*
Using constructors to create new objects
Constructor using NanoSmart called
Company: NanoSmart Shares: 12
Shares Price: $ 20.000 Total Worth: $ 240.00
Constructor using Boffo Objects called
Company: Boffo Objects Shares: 2
Shares Price: $ 2.000 Total Worth: $ 4.00
assigning stock1 to stock2:
Listening stock1 and stock2:
Company: NanoSmart Shares: 12
Shares Price: $ 20.000 Total Worth: $ 240.00
Company: NanoSmart Shares: 12
Shares Price: $ 20.000 Total Worth: $ 240.00
Using a constructor to reset an object
Constructor using Nifty foods called
Bye, Nifty foods!
Constructor using hhjh called
Constructor using aaa called
Bye, aaa!
Revised stock1:
Company: Nifty foods Shares: 10
Shares Price: $ 50.000 Total Worth: $ 500.00
Done
Bye, aaa!
Bye, NanoSmart!
Bye, Nifty foods!

Process returned 0 (0x0)   execution time : 0.724 s
Press any key to continue.

*/



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值