| |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
-> C++知识库 -> C++实验:类与对象 -> 正文阅读 |
|
[C++知识库]C++实验:类与对象 |
1、实验内容6) 定义一个类Stock,记录一支股票交易的基本信息,信息包括交易日序号(表示本月的第几个交易日,用整数表示)、当日最高价、当日最低价、当日开盘价和当日收盘价。尽量发挥想象力,为该类设计成员函数。 ??? 7) 在主函数中建立两个股票对象,分别存储该股票昨天和今天两天的信息,以当日收盘价计算该股票今天的涨幅。 ??? 8) 使用股票类Stock定义一个对象数组存放连续5个交易日的股票信息。编写一个主函数,计算两个Stock对象(前后两日)的当日收盘价,计算当日涨幅。用指针引用对象数组中的两个对象。在主函数中调用该函数计算从第2个交易日开始每天的当日涨幅。 ??? 9) 在Stock类中定义一个静态数据成员,记录当前Stock对象的数量。 ??? 10) 设计一个成员函数Assign_stock为对象赋值,其中的形式参数是对另一个Stock对象的引用,使用this指针避免对自己的赋值,在主函数中显示用Assign_stock赋值的对象。 11) 定义一个友元函数计算Stock对象的当日开盘价是否高于当日收盘价。若是,返回真;否则,返回假。 2、代码6) 定义一个类Stock,记录一支股票交易的基本信息,信息包括交易日序号(表示本月的第几个交易日,用整数表示)、当日最高价、当日最低价、当日开盘价和当日收盘价。尽量发挥想象力,为该类设计成员函数。 #pragma once #include<iostream> using namespace std; class Stock { private: ?????? int Number; ?????? double Max; ?????? double Min; ?????? double Begin; ?????? double End; public: ?????? Stock(); ?????? Stock(int n, double ma, double mi, double b, double e); ?????? double Get_End(); ?????? void Show_Stock(); }; #include "Stock.h" Stock::Stock() { ???????? Number = 0; ???????? Max = 0; ???????? Min = 0; ???????? Begin = 0; ???????? End = 0; } double Stock::Get_End() { ???????? return End; } Stock::Stock(int n, double ma, double mi, double b, double e) { ???????? Number = n; ???????? Max = ma; ???????? Min = mi; ???????? Begin = b; ???????? End = e; } void Stock::Show_Stock() { ???????? cout << Number << endl; ???????? cout << Max << endl; ???????? cout << Min << endl; ???????? cout << Begin << endl; ???????? cout << End << endl; } 7) 在主函数中建立两个股票对象,分别存储该股票昨天和今天两天的信息,以当日收盘价计算该股票今天的涨幅。 #pragma once #include<iostream> using namespace std; class Stock { private: ?????? int Number; ?????? double Max; ?????? double Min; ?????? double Begin; ?????? double End; public: ?????? Stock(); ?????? Stock(int n, double ma, double mi, double b, double e); ?????? double Get_End(); ?????? void Show_Stock(); }; #include "Stock.h" Stock::Stock() { ???????? Number = 0; ???????? Max = 0; ???????? Min = 0; ???????? Begin = 0; ???????? End = 0; } double Stock::Get_End() { ???????? return End; } Stock::Stock(int n, double ma, double mi, double b, double e) { ???????? Number = n; ???????? Max = ma; ???????? Min = mi; ???????? Begin = b; ???????? End = e; } void Stock::Show_Stock() { ???????? cout << Number << endl; ???????? cout << Max << endl; ???????? cout << Min << endl; ???????? cout << Begin << endl; ???????? cout << End << endl; } #include <iostream> #include "Stock.h" using namespace std; int main() { ?????? double rate; ?????? Stock s1(1, 3.2, 2.3, 2.5, 3.1); ?????? Stock s2(2, 4.2, 3.6, 3.9, 3.8); ?????? rate = (s2.Get_End() - s1.Get_End()) / s1.Get_End() * 100; ?????? cout << "以当日收盘价计算该股票今天的涨幅为:" << rate << "%" << endl; } 8) 使用股票类Stock定义一个对象数组存放连续5个交易日的股票信息。编写一个主函数,计算两个Stock对象(前后两日)的当日收盘价,计算当日涨幅。用指针引用对象数组中的两个对象。在主函数中调用该函数计算从第2个交易日开始每天的当日涨幅。 #pragma once #include<iostream> using namespace std; class Stock { private: ?????? int Number; ?????? double Max; ?????? double Min; ?????? double Begin; ?????? double End; public: ?????? Stock(); ?????? Stock(int n, double ma, double mi, double b, double e); ?????? double Get_End(); ?????? void Show_Stock(); ?????? void Set_Stock(); }; #include "Stock.h" Stock::Stock() { ?????? Number = 0; ?????? Max = 0; ?????? Min = 0; ?????? Begin = 0; ?????? End = 0; } double Stock::Get_End() { ?????? return End; } Stock::Stock(int n, double ma, double mi, double b, double e) { ?????? Number = n; ?????? Max = ma; ?????? Min = mi; ?????? Begin = b; ?????? End = e; } void Stock::Show_Stock() { ?????? cout << Number << endl; ?????? cout << Max << endl; ?????? cout << Min << endl; ?????? cout << Begin << endl; ?????? cout << End << endl; } void Stock::Set_Stock() { ?????? cout << "Number:"; ?????? cin >> Number; ?????? cout << "Max:"; ?????? cin >> Max; ?????? cout << "Min:"; ?????? cin >> Min; ?????? cout << "Begin:"; ?????? cin >> Begin; ?????? cout << "End:"; ?????? cin >> End; } #include <iostream> #include "Stock.h" using namespace std; int main() { ?????? int i; ?????? Stock s[5],*p; ?????? for (i = 0,p=s; i < 5; i++,p++) ?????? { ????????????? p->? Set_Stock(); ?????? } ?????? for (i = 0,p = s; i < 5; i++) ?????? { ????????????? s->Show_Stock(); ?????? } ?????? for (i = 1,p=s+1; i < 5; i++,p++) ?????? { ????????????? ????????????? cout << "涨幅为:" << ((p->Get_End() - (p-1)->Get_End()) / (p-1)->Get_End() * 100) << "%"<<endl; ?????? } } 9) 在Stock类中定义一个静态数据成员,记录当前Stock对象的数量。 #pragma once #include<iostream> using namespace std; class Stock { private: ?????? static int count; ?????? int Number; ?????? double Max; ?????? double Min; ?????? double Begin; ?????? double End; public: ?????? Stock(); ?????? Stock(int n, double ma, double mi, double b, double e); ?????? double Get_End(); ?????? void Show_Stock(); ?????? void Set_Stock(); ?????? int Count(); }; #include "Stock.h" int Stock::count = 0; Stock::Stock() { ?????? count++; ?????? Number = 0; ?????? Max = 0; ?????? Min = 0; ?????? Begin = 0; ?????? End = 0; } double Stock::Get_End() { ?????? count++; ?????? return End; } Stock::Stock(int n, double ma, double mi, double b, double e) { ?????? count++; ?????? Number = n; ?????? Max = ma; ?????? Min = mi; ?????? Begin = b; ?????? End = e; } void Stock::Show_Stock() { ?????? count++; ?????? cout << Number << endl; ?????? cout << Max << endl; ?????? cout << Min << endl; ?????? cout << Begin << endl; ?????? cout << End << endl; } void Stock::Set_Stock() { ?????? count++; ?????? cout << "Number:"; ?????? cin >> Number; ?????? cout << "Max:"; ?????? cin >> Max; ?????? cout << "Min:"; ?????? cin >> Min; ?????? cout << "Begin:"; ?????? cin >> Begin; ?????? cout << "End:"; ?????? cin >> End; } int Stock::Count() { ?????? count++; ?????? return count; } #include <iostream> #include "Stock.h" using namespace std; int main() { ?????? int i; ?????? Stock s[5],*p; ?????? for (i = 0,p=s; i < 5; i++,p++) ?????? { ????????????? p->? Set_Stock(); ?????? } ?????? for (i = 0,p = s; i < 5; i++) ?????? { ????????????? s->Show_Stock(); ?????? } ?????? for (i = 1,p=s+1; i < 5; i++,p++) ?????? { ????????????? cout << "涨幅为:" << ((p->Get_End() - (p-1)->Get_End()) / (p-1)->Get_End() * 100) << "%"<<endl; ????????????? cout << "当前Stock的对象的个数为:" << p->Count() << endl; ?????? } } 10) 设计一个成员函数Assign_stock为对象赋值,其中的形式参数是对另一个Stock对象的引用,使用this指针避免对自己的赋值,在主函数中显示用Assign_stock赋值的对象。 #pragma once #include<iostream> using namespace std; class Stock { private: ?????? static int count; ?????? int Number; ?????? double Max; ?????? double Min; ?????? double Begin; ?????? double End; public: ?????? Stock(); ?????? Stock(int n, double ma, double mi, double b, double e); ?????? double Get_End(); ?????? void Show_Stock(); ?????? void Set_Stock(); ?????? int Count(); ?????? void Assign_stock(Stock& p); }; #include "Stock.h" int Stock::count = 0; Stock::Stock() { ?????? count++; ?????? Number = 0; ?????? Max = 0; ?????? Min = 0; ?????? Begin = 0; ?????? End = 0; } double Stock::Get_End() { ?????? count++; ?????? return End; } Stock::Stock(int n, double ma, double mi, double b, double e) { ?????? count++; ?????? Number = n; ?????? Max = ma; ?????? Min = mi; ?????? Begin = b; ?????? End = e; } void Stock::Show_Stock() { ?????? count++; ?????? cout << Number << endl; ?????? cout << Max << endl; ?????? cout << Min << endl; ?????? cout << Begin << endl; ?????? cout << End << endl; } void Stock::Set_Stock() { ?????? count++; ?????? cout << "Number:"; ?????? cin >> Number; ?????? cout << "Max:"; ?????? cin >> Max; ?????? cout << "Min:"; ?????? cin >> Min; ?????? cout << "Begin:"; ?????? cin >> Begin; ?????? cout << "End:"; ?????? cin >> End; } int Stock::Count() { ?????? count++; ?????? return count; } void Stock::Assign_stock(Stock& p) { ?????? if (this != &p) ?????? { ????????????? Number = p.Number; ????????????? Max = p.Max; ????????????? Min = p.Min; ????????????? Begin = p.Begin; ????????????? End = p.End; ?????? } } #include <iostream> #include "Stock.h" using namespace std; int main() { ?????? Stock s1(3, 7.4, 6.5, 6.65, 7.23); ?????? Stock s2; ?????? s2.Assign_stock(s1); ?????? s2.Show_Stock(); } 11) 定义一个友元函数计算Stock对象的当日开盘价是否高于当日收盘价。若是,返回真;否则,返回假。 #pragma once #include<iostream> using namespace std; class Stock { private: ?????? static int count; ?????? int Number; ?????? double Max; ?????? double Min; ?????? double Begin; ?????? double End; public: ?????? Stock(); ?????? Stock(int n, double ma, double mi, double b, double e); ?????? double Get_End(); ?????? void Show_Stock(); ?????? void Set_Stock(); ?????? int Count(); ?????? void Assign_stock(Stock& p); ?????? friend int GetStock(Stock& s1); }; #include "Stock.h" int Stock::count = 0; Stock::Stock() { ?????? count++; ?????? Number = 0; ?????? Max = 0; ?????? Min = 0; ?????? Begin = 0; ?????? End = 0; } double Stock::Get_End() { ?????? count++; ?????? return End; } Stock::Stock(int n, double ma, double mi, double b, double e) { ?????? count++; ?????? Number = n; ?????? Max = ma; ?????? Min = mi; ?????? Begin = b; ?????? End = e; } void Stock::Show_Stock() { ?????? count++; ?????? cout << Number << endl; ?????? cout << Max << endl; ?????? cout << Min << endl; ?????? cout << Begin << endl; ?????? cout << End << endl; } void Stock::Set_Stock() { ?????? count++; ?????? cout << "Number:"; ?????? cin >> Number; ?????? cout << "Max:"; ?????? cin >> Max; ?????? cout << "Min:"; ?????? cin >> Min; ?????? cout << "Begin:"; ?????? cin >> Begin; ?????? cout << "End:"; ?????? cin >> End; } int Stock::Count() { ?????? count++; ?????? return count; } void Stock::Assign_stock(Stock& p) { ?????? if (this != &p) ?????? { ????????????? Number = p.Number; ????????????? Max = p.Max; ????????????? Min = p.Min; ????????????? Begin = p.Begin; ????????????? End = p.End; ?????? } } int GetStock(Stock&s1) { ?????? cout << s1.Begin << "\t" << s1.End; ?????? if ((s1.Begin) > (s1.End)) ????????????? return 1; ?????? else return 0; } #include <iostream> #include "Stock.h" using namespace std; int main() { ?????? int i; ?????? Stock s[5], * p; ?????? for (i = 0, p = s; i < 5; i++, p++) ?????? { ????????????? p->Set_Stock(); ?????? } ?????? for (i = 0, p = s; i < 5; i++) ?????? { ????????????? s->Show_Stock(); ?????? } ?????? for (i = 1, p = s + 1; i < 5; i++, p++) ?????? { ????????????? cout << "涨幅为:" << ((p->Get_End() - (p - 1)->Get_End()) / (p - 1)->Get_End() * 100) << "%" << endl; ????????????? cout << "当前Stock的对象的个数为:" << p->Count() << endl; ?????? } ?????? for (i = 0, p = s; i < 5; i++, p++) ?????? { ????????????? cout << GetStock(*p) << endl; ?????? } } |
|
C++知识库 最新文章 |
【C++】友元、嵌套类、异常、RTTI、类型转换 |
通讯录的思路与实现(C语言) |
C++PrimerPlus 第七章 函数-C++的编程模块( |
Problem C: 算法9-9~9-12:平衡二叉树的基本 |
MSVC C++ UTF-8编程 |
C++进阶 多态原理 |
简单string类c++实现 |
我的年度总结 |
【C语言】以深厚地基筑伟岸高楼-基础篇(六 |
c语言常见错误合集 |
|
上一篇文章 下一篇文章 查看所有文章 |
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 | -2024/11/24 4:05:08- |
|
网站联系: qq:121756557 email:121756557@qq.com IT数码 |