c++的this指针和对象数组
来源:互联网 发布:ipad4软件更新打不开 编辑:程序博客网 时间:2024/05/01 23:38
1.每个成员函数(包括构造函数和析构函数)都有一个this指针,this指针指向当前调用对象。如果需要调用整个对象,则使用*this,*this表示整个对象。
2.对象数组
●对象数组的定义
Stock flutty[4];●对象数组的初始化
Stock flutty[4]; //此时调用默认构造函数进行初始化
Stock stocks[3] = { Stock("Nano", 123, 12.3), Stock("Boffo", 233, 11.2), Stock("Fleep", 456, 13.8)}; //调用构造函数进行初始化3.this指针使用实例:
下例使用this指针比较对象数组中价格这个成员的大小
文件结构:
stock20.h代码:
#ifndef STOCK10_H_INCLUDED#define STOCK10_H_INCLUDED#include <string>using namespace std;class Stock{ private: string company; long shares; float price; float total; void setTotal() { total = shares * price; } public: Stock(); //默认构造函数 Stock(const string & comp, long shar, float pric); //构造函数 ~Stock(); //析构函数 void buy(long shar, float pric); void sell(long shar, float pric); void show(); const Stock & topVal(const Stock & s) const;};#endif // STOCK10_H_INCLUDEDstock20.cpp代码:
#include <iostream>#include "stock20.h"using namespace std;Stock::Stock() //定义默认构造函数{ company = "No name"; shares = 0; price = 0.0; setTotal();}Stock::Stock(const string & comp, long shar, float pric) //定义构造函数{ company = comp; shares = shar; price = pric; setTotal();}Stock::~Stock() //析构函数{}void Stock::buy(long shar, float pric){ if(shar < 0) cout << "Cannot buy a nagitive number of shares.\n"; else shares += shar; price = pric; setTotal();}void Stock::sell(long shar, float pric){ if(shar < 0) cout << "Cannot sell a nagitive number of shares.\n"; else if(shares < shar) cout << "Cannot sell a larger number of shares.\n"; else shares -= shar; price = pric; setTotal();}void Stock::show(){ cout << "Company : " << company << endl; cout << "Shares : " << shares << endl; cout << "Price : " << price << endl; setTotal(); cout << "Total : " << total << endl << endl;}const Stock & Stock::topVal(const Stock & s) const{ if(s.total > total) return s; else return *this; //返回当前对象}main.cpp代码:
#include <iostream>#include "stock20.h"using namespace std;int main(){ int i; Stock flutty[4]; for(i = 0; i < 4; i++) flutty[i].show(); for(i = 0; i < 4; i++) flutty[i].buy(1000 * i, 12.3 * i); for(i = 0; i < 4; i++) flutty[i].show(); Stock top; top = flutty[0]; for(i = 1; i < 4; i++) top = top.topVal(flutty[i]); //调用topVal函数进行比较大小 cout << "The highest price shares is \n"; top.show(); return 0;}Stock stocks[3] = { Stock("Nano", 123, 12.3), Stock("Boffo", 233, 11.2), Stock("Fleep", 456, 13.8)};运行结果:
0 0
- c++的this指针和对象数组
- this指针和对象数组
- C++:对象数组与this指针
- C++:对象数组、对象动态申请和释放、类的定义和实现分开、this指针、常成员函数、时钟
- c++对象数组和this指针
- C++---对象数组和对象指针
- 对象数组与指针+this指针
- C++this指针的理解和用法
- C++this指针的理解和用法
- 【c++】this 指针和类的继承
- C ++的this指针
- C语言指针、数组指针和指针数组的区别
- c/c++--指针数组和数组指针的区别
- 第二十章,对象指针及this的使用(C++)
- C++对象指针和this指针~
- 【C/C++】数组和指针的区别
- C++的类对象的sizeof值和this指针
- 指针和对象的数组实现
- 计算几何模板——不断更新
- mongo Dao层 增删改查方法
- vnc图形界面配置
- 保存网格(mesh)到磁盘上
- Oracle中null的使用详解
- c++的this指针和对象数组
- ERDAS IMAGINE 2013裁剪影像方法
- 电脑小技巧70个
- Android Uri、UriMatcher、ContentUris详解
- 获取农历节日.节气
- spark internal - 作业调度
- java初学感谢三
- 2014校选题(六)--简单数学题
- 测试备库应用主库日志时有无using current logfile选项的区别