赞
踩
举个例子,在我们完成小组任务中,一般会有两种分配任务的方法:
(1)根据每个人(即每个对象)擅长的工作,分配相应的任务(即对象的行为);
(2)分析出解决问题所需要的步骤,每人完成一个步骤(功能函数)。
方法(1)就是面向对象的设计思想,将问题分解成各个对象,每个对象有确定的行为,而不是去完成一个步骤。那么方法(2)就是面向过程的设计思想,每人通过函数将实现一个步骤(因为不一定是自身擅长的,那么每个人就不重要了,重要的是每个步骤),使用的时候调用相应的功能函数即可。
C++就是一种面向对象的语言。
(1)类:具有同种属性的对象。例如:


(2)类的四大特性:封装性、继承性、多态性、抽象性。
这里先讲一个封装性:一般对象都具有属性和行为。对于一个人,身高、年龄、性别是他的属性,吃饭、睡觉是他的行为,我们将一个对象的属性和方法封装在一个类,即为封装。
C++中使用关键字class来定义类,以上述第二个为例,格式如下:
- class Goods //类名--商品
- {
- public:
- //公共的行为或属性,所有人都可以访问
- string Name; //名称
- private:
- //私有的行为或属性,只能该类内部访问
- int Amount; //数量
- float Price; //单价
- protected:
- //被保护的行为或属性,不能被外界访问,但是能被子类直接访问
- //子类:继承了父类的属性和方法,也可以添加自己的属性的方法,后面会讲哦~
- float Total; //总价
- };
说明:
(1)类名、属性名在命名时尽量见名知意。
(2)花括号中的部分称为类体,类体中定义了类的属性。
(3)访问限定符有三种: public(公共的), private(私有的)和protected(保护的)。只有public说明的成员能从外部进行访问,每种说明符可在类体中使用多次。
(4)访问说明符 private和 protected 体现了类具有封装性。
(5)切记花括号后面的分号不能省略。
类的实现有两种方法:(1)成员函数在类体内定义;(2)成员函数在类体内声明,类外定义。
(1)在类体内定义方法,格式如下:
- #include<iostream>
- #include<string>
- using namespace std;
- class Goods
- {
- private:
- string Name;
- int Amount;
- float Price;
- float Total;
- public:
- void RegisterGoods(const string& name, int num, float price) //初始化数据
- {
- Name = name;
- Amount = num;
- Price = price;
- CountTotal();
- }
- void CountTotal() //计算商品总价值
- {
- Total = Amount * Price;
- }
- string GetName() //读取商品名
- {
- return Name;
- }
- };
-
- int main()
- {
- Goods book; //用定义好的类创建了一个对象 book
- book.RegisterGoods("父与子 ", 20, 25.5f); //初始化book的属性
- cout << book.GetName() << endl; //输出book的名称
- }

说明:在类内直接定义成员函数,在编译阶段编译器会默认其为inline(内联)型函数。
(2)在类体内声明,类外定义,格式如下:
- #include<iostream>
- #include<string>
- using namespace std;
- class Goods
- {
- private:
- string Name;
- int Amount;
- float Price;
- float Total;
- public:
- void RegisterGoods(const string&, int, float); //声明可以给形参,可以不给
- void CountTotal();
- float GetTotal();
- };
-
- //在类外定义需要加作用域解析符
- void Goods::RegisterGoods(const string& name, int num, float price)
- {
- Name = name;
- Amount = num;
- Price = price;
- CountTotal();
- }
- void Goods::CountTotal()
- {
- Total = Amount * Price;
- }
- float Goods::GetTotal()
- {
- return Total;
- }
-
- int main()
- {
- Goods book; //用定义好的类创建了一个对象 book
- book.RegisterGoods("父与子 ", 20, 25.5f); //初始化book的属性
- cout << book.GetTotal() << endl; //510
- }

说明:
有下面一个例子:
- #include<iostream>
- #include<string>
- using namespace std;
- class Goods
- {
- private:
- string Name;
- int Amount;
- float Price;
- float Total;
- public:
- void RegisterGoods(string&, int, float); //初始化数据
- void CountTotal(); //计算商品总价值
- float GetTotal(); //读取商品总价值
- };
-
- void Goods::RegisterGoods(string name, int amount, float price)
- {
- Name = name;
- Amount = amount;
- Price = price;
- }
- void Goods::CountTotal()
- {
- Total= Price * Amount;
- }
- float Goods::GetTotal()
- {
- return Total;
- }
-
- int main()
- {
- Goods book, pen;
- book.RegisterGoods("book", 12, 10.5);
- book.CountTotal();
- cout << book.GetTotal() << endl; //126
- pen.RegisterGoods("pen", 20, 7.5);
- pen.CountTotal();
- cout << pen.GetTotal() << endl; //150
- return 0;
- }

- #include<iostream>
- #include<string>
- using namespace std;
- class Goods
- {
- private:
- string Name;
- int Amount;
- float Price;
- float Total;
- public:
- //void RegisterGoods(Goods* this,string&,int,float);
- void RegisterGoods(string&, int, float); //初始化数据
- //void CountTotal(Goods* this);
- void CountTotal(); //计算商品总价值
- //float GetTotal(Goods* this);
- float GetTotal(); //读取商品总价值
- };
-
- //void RegisterGoods(Goods* this,string&,int,float);
- void Goods::RegisterGoods(string name, int amount, float price)
- {
- Name = name; //this->Name = name;
- Amount = amount; //this->Amount = amount;
- Price = price; //this->Price = price;
- }
-
- //void CountTotal(Goods* this);
- void Goods::CountTotal()
- {
- //this->Total = this->Amount * this->Price;
- Total= Price * Amount;
- }
-
- //float GetTotal(Goods* this);
- float Goods::GetTotal()
- {
- return Total;
- //return this->Total;
- }
-
- int main()
- {
- Goods book, pen;
- book.RegisterGoods("book", 12, 10.5);
- book.CountTotal(); //CountTotal(&book);
- cout << book.GetTotal() << endl; //126
- pen.RegisterGoods("pen", 20, 7.5);
- pen.CountTotal(); //CountTotal(&pen);
- cout << pen.GetTotal() << endl; //150
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。