C++ 练习笔记

来源:互联网 发布:剑三初始捏脸数据 编辑:程序博客网 时间:2024/06/03 16:01

1.派生类对象可以访问基类的成员函数和成员对象,但是基类不可以访问派生类的成员函数和对象。

2.常量成员 可以访问常量成员函数,而不可以访问非常量成员函数。

3.抽象类不可以实例化对象,只能作为接口,成员函数和对象只能通过派生类成员对象访问。

4.公有继承,基类的保护成员可以被派生类访问。

5.派生类的构造函数要实现调用基类的构造函数(注意标有notice的 语句)。

6.在派生类中的构造函数声明的时候,要把基类构造函数的参数加上(注意标有notice的 语句)

7.如果派生类在类外实现构造函数,那么只有在派生类构造函数定义的地方列出基类的构造函数即可,不是在声明的时候列出(注意标有notice的 语句)

8.基类的虚函数必须有定义才可以被重载,如果只是声明虚函数,则能编译通过,但是不会生成。会出现不能解析的外部符号错误。纯虚函数可以只声明,不用定义就可以实现重载。

<pre name="code" class="cpp"><span style="font-size:14px;">// stock2.h -- augmented version#ifndef STOCK_H_#define STOCK_H_#include "General.h"class BaseStock{public:BaseStock(int m):Sum(m){NewName=NULL;printf("%s\n,%d\n","BaseStock",Sum);}   //base constructor~BaseStock(){}bool  BaseSave(char *ss, int SaveNum);int BaseSaveNum;virtual int BaseReport(int t=0,int n=0 );virtual bool RenameCompany(char *s)=0;    // pure virtual functionprotected:char *NewName;int Sum;private:};class Stock:public BaseStock{private:    char company[30];    int shares;    double share_val;    double total_val;    void set_tot() { total_val = shares * share_val; }public:Stock(int m);         // default constructor    notice   parameter  mStock(int m,const char * co, int n = 0, double pr = 0.0);     //notice   parameter  m    ~Stock();       // do-nothing destructor    void buy(int num, double price);    void sell(int num, double price);    void update(double price);    void show()const;    const Stock & topval(const Stock & s) const;virtual int BaseReport(int t=0,int n=0 );virtual bool RenameCompany(char *s);    //override  pure virtual function };#endif// Stock.c#include <iostream>#include "stock.h"//#include "General.h"//base classint BaseStock::BaseReport(int a, int b){std::cout<<a-b<<std::endl;return (a-b);}bool  BaseStock::BaseSave(char *ss, int SaveNum){NewName=new char[20];std::strncpy(NewName,ss,20);std::cout<<NewName<<"\n"<<std::endl;if (NewName==NULL){return false;}else {return true;}}// derive class  function//constructorsStock::Stock(int m):BaseStock(m)            //call base constructor{    std::strcpy(company, "no name");    printf("%s\n",company);    shares = 0;    share_val = 0.0;    total_val = 0.0;}Stock::Stock(int m, const char * co, int n, double pr):BaseStock(m)  //call base constructor{    std::strncpy(company, co, 29);     company[29] = '\0';printf("%s\n",company);    if (n < 0)    {        std::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 destructorStock::~Stock()        // quiet class destructor{}// other methodsvoid Stock::buy(int num, double price){     if (num < 0)    {        std::cout << "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::cout;    if (num < 0)    {        cout << "Number 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;Sum=50;       //class  BaseStock's   projected member can  be called  by  derived class  function     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; }//derived virtual functionint Stock::BaseReport(int a,int b){printf("%d\n",a+b);return a+b;}//override pure  virtual functionbool Stock::RenameCompany(char *s){std::strncpy(NewName,s,20);std::cout<<NewName<<std::endl;Sum=50;if (NewName==NULL){return false;}else {return true;}}int main(void){const Stock S1_object(10,"zhang",5,250);   //noticeS1_object.show();//BaseStock *B_sobject=new  BaseStock;     //abstract class  can't  constantiate object//B_sobject->BaseReport(9,7);  //B_sobject->show();       //Base class can't  call derived class function Stock *S_object=new Stock(10);            //noticeS_object->show();S_object->BaseReport(9,7);S_object->BaseSave("Ling",20);          //Derived class can  call base class function S_object->RenameCompany("wangling");delete S_object;return (0);}</span>


0 0