C++第十章__类和对象定义的基础知识

目录

类和对象定义的基础知识 m1

//通过一个例子来说明如何创建类和对象
//类声明通常在h文件完成,通常将类的首字母大写

//stock00.h 程序说明
01)下述程序用关键字class定义了一个类Stock,这种语法指出Stock是一个新类的类型名。该声明让我们能够
  声明Stock类型的变量---成为对象或实例。例如:
  Stock sally; //声明一个类对象sally
  Stock solly; //声明一个类对象solly
02)要存储的数据以类数据成员(如company和shares)的形式出现。
  要执行的操作以类函数成员(如set_tot())的形式出现。成员函数可以就地定义(如set_tot()),也可以用原型
  表示。
03)将数据和方法(函数)组成一个单元是类最吸引人的地方。
04)类和对象都可以直接访问共有部分(public部分),但是对于私有部分,只能通过共有成员函数(如update()和show())
  来访问。
05)类设计尽可能将共有接口(public中定义的函数)和实现细节(private中声明的数据)分开
06)OOP特性:
  A 首先将数据表示和函数原型放在一个类声明中(而不是一个文件中),通过类声明将所有的内容放在一个来声明中
      来使描述成为一个整体
  B 让数据表示成为私有,使得数据只能通过被授权函数来访问,被授权函数即public中声明的函数
07)由于隐藏数据是OOP追求的目标之一,所以一般是将数据放在private中,将函数声明放在public中。但是也可以
  在private中声明和定义函数,该函数就只能在共有方法(public中声明的函数)中使用
08)OOP和结构的区别:
  结构默认的访问方式是pulic,而类为private。c++程序员一般通过使用类来实现类描述,而把结构限制为只表示
  纯粹的数据对象

 1 #ifndef STOCK00_H_    //编译器只编译一次
 2 #define STOCK00_H_  
 3 
 4 #include <string>
 5 
 6 class Stock  //类声明,只能用class
 7 {
 8 private:     //private可以不用写,因为这是类对象默认的访问控制,但是也可以写上的
 9     std::string company;  //定义string类变量company
10     long shares;
11     double share_val;
12     double total_val;
13     void set_tot()  //定义函数set_tot(),在类声明中定义的函数,将自动成为内联函数
14     {
15         total_val = share_val * shares;
16     }
17 public:
18     void acquire(const std::string & co, long n, double pr);  //声明函数
19     void buy(long num, double price);  //声明函数
20     void sell(long num, double price);  //声明函数
21     void update(double price);  //声明函数
22     void show();  //声明函数
23 };  //不要忘记分号
24 
25 #endif
stock.h

stock00.cpp程序说明

01)定义成员函数时,使用作用域解析运算符::来表示函数所属的类
 例如:
 void Stock::update(double price)
 void Buffoon::update()
 其中这两个update()是不一样的,且这样声明和定义不会报错
02)类方法可以访问类的private中的组件(声明的变量和定义的函数)
  如在上面的public中声明的成员函数中都使用了Stock中private下声明的变量
03)使数据私有并限于对公有函数访问的技术允许我们能够控制数据如何被使用

/* 内联方法 */
/*
01)其定义位于类声明中的函数都将自动成为内联函数
02)也可以在类声明之外定义成员函数,并使之成为内联函数,为此只需使用关键字inline即可
  class Stock
  {
  private:
    ...
    void set_tot(); //只是声明,没有定义,此时set_tot()还并未是内联函数
  public:
    ...
  };

  inline void Stock::ste_tot()
  {
    total = shares * share_val;
  }

 1 #include <iostream>
 2 #include "Stock00.h"
 3 
 4 //定义成员函数时,使用作用域解析运算符::来表示函数所属的类
 5 //类方法可以访问类的private中的组件(声明的变量和定义的函数)
 6 void Stock::acquire(const std::string & co, long n, double pr) //使用::来表示Stock类下的acquire()函数
 7 {
 8     company = co;  //为Stock类中private中的数据变量company赋值
 9     if (n < 0)
10     {
11         std::cout << "Number of shares can't be negative; " << company << " shares set to 0" << std::endl;
12         shares = 0;  //为Stock类中private中的数据变量shares赋值
13     }
14     else
15         shares = n;  //为Stock类中private中的数据变量shares赋值
16     share_val = pr;  //为Stock类中private中的数据变量share_val赋值
17     set_tot();  //调用在Stock类中private下定义的函数,且该函数只能是在公有成员函数中调用,在别的地方调用的话会报错
18 }
19 
20 void Stock::buy(long num, double price)
21 {
22     if (num < 0)
23     {
24         std::cout << "Number of shares purchased can't be negative; "
25             << "Transaction is aborted" << std::endl;
26     }
27     else
28     {
29         shares += num;  //为Stock类中private中的数据变量shares赋值
30         share_val = price;  //为Stock类中private中的数据变量share_val赋值
31         set_tot();  //调用在Stock类中private下定义的函数
32     }
33 }
34 
35 void Stock::sell(long num, double price)
36 {
37     using std::cout;
38     using std::endl;
39     if (num < 0)
40     {
41         cout << "Number of shares sold can't be negetive. "
42             << "Transaction is aborted" << endl;
43     }
44     else if(num>shares)
45     {
46         cout << "You can't sell more than you have. "
47             << "Transaction is aborted. " << endl;
48     }
49     else
50     {
51         shares -= num;  //为Stock类中private中的数据变量shares赋值
52         share_val = price;  //为Stock类中private中的数据变量share_val赋值
53         set_tot();  //调用在Stock类中private下定义的函数
54     }
55 }
56 
57 void Stock::update(double price)
58 {
59     share_val = price;   //为Stock类中private中的数据变量share_val赋值
60     set_tot();  //调用在Stock类中private下定义的函数
61 }
62 
63 //原版本如下
64 //void Stock::show()
65 //{
66 //    using std::cout;
67 //    using std::endl;
68 //    cout << "Company: " << company
69 //        << "Shares: " << shares<<'\n'      //'\n'表示输出换行
70 //        << "share Price: $" << share_val
71 //        << "Total Worth" << total_val << endl;
72 //}
73 
74 //修改版本如下
75 void Stock::show()
76 {
77     using std::cout;
78     using std::endl;
79     using std::ios_base;
80     ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield); //定义变量orig
81     //fmtflags是一种在ios_base中的数据类型
82     std::streamsize prec = cout.precision(3);  //定义变量prec 
83     //orig和prec都是为了保持原始的输出状态
84     cout << "Company: " << company
85         << "Shares: " << shares << '\n';      //'\n'表示输出换行
86 
87     cout.precision(2);  //set format to #.##
88 
89     cout << "share Price: $" << share_val
90          << "Total Worth" << total_val << endl;
91     
92     //恢复原始的输出状态
93     cout.setf(orig, ios_base::floatfield);
94     cout.precision(prec);
95 }
stock00.cpp
 1 //使用类
 2 //stock_main()
 3 
 4 #include <iostream>
 5 #include "stock00.h"
 6 
 7 int main()
 8 {
 9     Stock fluffy_the_cat;  //使用类Stock创建类对象fluffy_the_cat
10 
11     fluffy_the_cat.acquire("NanoSmart", 20, 12.50); //使用类对象fluffy_the_cat调用类Stock中的acquire()方法
12     fluffy_the_cat.show();  //使用类对象fluffy_the_cat调用类Stock中的show()方法
13 
14     fluffy_the_cat.buy(15, 18.125);  //使用类对象fluffy_the_cat调用类Stock中的buy()方法
15     fluffy_the_cat.show();
16 
17     fluffy_the_cat.sell(400, 20.00);  //使用类对象fluffy_the_cat调用类Stock中的sell()方法
18     fluffy_the_cat.show();
19 
20     fluffy_the_cat.buy(300000, 40.125);  //Stock类中的buy()方法第一个形参为long型,第二个为double型
21     fluffy_the_cat.show();
22 
23     fluffy_the_cat.sell(300000, 0.125);  //使用类对象fluffy_the_cat调用类Stock中的sell()方法
24     fluffy_the_cat.show();
25 
26     system("pause");
27     return 0;
28 }
stcok_main.cpp

程序执行结果:

猜你喜欢

转载自www.cnblogs.com/YiYA-blog/p/10657012.html