c++: 定义一个日期类,包含年月日信息,分别使用友元函数和成员函数的方式,实现对 --运算符的重载(注意前置和后置的区别) …
- User
c++: 定义一个日期类,包含年月日信息,分别使用友元函数和成员函数的方式,实现对 --运算符的重载(注意前置和后置的区别)
提示:自减的操作为day减1,减完注意逻辑上要判断,day是否减成0了,如果减成0要修改成前一个月的最后一天,month也要改成前一个月;再对month做判断,如果减成0了,year也要修改
- Assistant
成上一年,month改为12月份。
- User
代码
- Assistant
//友元函数方式实现-- #include using namespace std;
class Date { private: int year, month, day; friend Date operator--(Date& date, int); public: Date(int y, int m, int d): year(y), month(m), day(d) {} void show() {cout << year << "-" << month << "-" << day << endl;} };
Date operator--(Date& date, int) { Date tmp(date.year, date.month, date.day); --date.day; if (date.day == 0) { --date.month; if (date.month == 0) { --date.year; date.month = 12; } date.day = 31; //默认一个月31天 } return tmp; }
int main() { Date d1(2021, 9, 1); d1.show(); d1--; d1.show(); return 0; }
//成员函数方式实现-- #include
using namespace std;
class Date { private: int year, month, day; public: Date(int y, int m, int d): year(y), month(m), day(d) {} Date operator--(int); void show() {cout << year << "-" << month << "-" << day << endl;} };
Date Date::operator--(int) { Date tmp(year, month, day); --day; if (day == 0) { --month; if (month == 0) { --year; month = 12; } day = 31; } return tmp; }
int main() { Date d1(2021, 9, 1); d1.show(); d1--; d1.show(); return 0; }
- User
利用函数模板设计一个找出数组元素中最小值的函数,并在主函数中测试
- Assistant
代码如下:
#include <iostream> using namespace std; template <typename T> T Min(T a[], int size) { T min_val = a[0]; for (int i = 1; i < size; i++) { if (a[i] < min_val) min_val = a[i]; } return min_val; } int main() { int arr_int[] = {3, 5, 2, 7, 1, 4}; double arr_double[] = {3.5, 5.2, 2.8, 7.9, 1.4, 4.6}; int size_int = sizeof(arr_int) / sizeof(int); int size_double = sizeof(arr_double) / sizeof(double); int min_int = Min(arr_int, size_int); double min_double = Min(arr_double, size_double); cout << "min_int: " << min_int << endl; cout << "min_double: " << min_double << endl; return 0; }输出结果:
min_int: 1 min_double: 1.4可以看到,利用函数模板实现了找出数组元素中最小值的函数,并在主函数中对int数组和double数组进行了测试。
- User
设计一个类模板, 其中包含数据成员Ta [10] (其数据类型用T来表示);
成员函数Sort ( ),用来实现将Ta[10]中的数据按照从小到大排序的功能;构造函数,用来对Ta[10]进行初始化;成员函数display(),用来实现Ta[10]各数据的输出。
【要求】:类外定义函数;在主函数中将模板类型参数T实例化为int,
并对建立的对象中Ta[10]的10个整数进行排序并输出。
十个整数请使用: 1,5,6 3,4,1,2,5,7,0、
- Assistant
代码如下:
#include<iostream> using namespace std; template <typename T> class Array { private: T Ta[10]; public: Array(T arr[]); void Sort(); void display(); }; template <typename T> Array<T>::Array(T arr[]) { for (int i = 0; i < 10; i++) { Ta[i] = arr[i]; } } template <typename T> void Array<T>::Sort() { for (int i = 0; i < 9; i++) { for (int j = i+1; j < 10; j++) { if (Ta[i] > Ta[j]) { T tmp = Ta[i]; Ta[i] = Ta[j]; Ta[j] = tmp; } } } } template <typename T> void Array<T>::display() { for (int i = 0; i < 10; i++) { cout << Ta[i] << " "; } cout << endl; } int main() { int arr[] = {1, 5, 6, 3, 4, 1, 2, 5, 7, 0}; Array<int> a(arr); a.Sort(); a.display(); return 0; }输出结果:
0 1 1 2 3 4 5 5 6 7可以看到,利用类模板定义了一个数组类 Array,其中将数据成员Ta[10] 作为 T 类型的数组。在类模板中定义了构造函数和 Sort() 函数、display() 函数,实现了按照从小到大的顺序对数组元素进行排序,并输出排序后的结果。在主函数中,将 int 类型作为模板类型参数,并对建立的对象中Ta[10]的10个整数进行排序并输出。
- User
代码
- Assistant
对输入日期,进行字符串转化为 Date 数据类型的处理:
#include <iostream> #include <stdlib.h> #include <string.h> using namespace std; class Date { public: int year, month, day; Date(int y, int m, int d): year(y), month(m), day(d) {} void show() {cout << year << "-" << month << "-" << day << endl;} }; Date strToDate(char* str) { int year = 0, month = 0, day = 0; char* p = strtok(str, "-"); year = atoi(p); p = strtok(NULL, "-"); month = atoi(p); p = strtok(NULL, "-"); day = atoi(p); return Date(year, month, day); } int main() { char str[] = "2021-09-01"; Date d = strToDate(str); d.show(); return 0; }输出结果:
2021-9-1利用 strtok() 函数将字符串按照 '-' 进行分割,查找年月日各自对应的数值,并利用 atoi() 函数将字符串转化为整数类型。最后使用 Date 类型的构造函数生成相应的 Date 类型对象 d。
- User
本题有5问,怕一下子接受不了,意思是循序渐进地添加内容,最终上交做完第5问的代码即可。
1) 定义一个商品类CGoods,其中包含商品号(long no)、商品名(char *p_name)、商品价格(double price)三个数据成员,以及相应的构造函数、复制构造函数、析构函数、打印数据成员的成员函数。
#include
#include <string.h>
using namespace std;
class CGoods
{
protected:
long no;
char *p_name;
double price;
public:
CGoods (long no_val, char * p_val, double price_val);
//提示:char数组长度可用strlen获得,注意strlen的结果是否包含\0了
CGoods (const CGoods & g ); //注意:不要创造浅拷贝
~CGoods (){delete [] p_name;}
void print() const;
};
2) 为CGoods类增加一个商品总数(int count)数据成员,并增加一个成员函数getCount()获取count的值,再编写一个友元函数getName()在类外获取输出商品名称p_name。
3) 为CGoods类定义小于运算符(‘<’)和不小于运算符(‘>=’)两个运算符重载函数。CGoods类对象大小的比较是根据其商品价格(price)的值的大小来实现的。
4) 以CGoods类为基类,派生出服装类CClothes和食品类CFood两个派生类,并在这两个类中分别增加一个表示品牌的指针数据成员(char *p_brand)和表示用途的成员函数(void usedFor()——函数体就是输出一条表示服装和食品用途的信息)。写出CClothes类和CFood类的完整定义(包括含参构造、复制构造、析构和usedFor()成员函数的实现)。
5)为了能够采用动态关联的方式调用派生类的usedFor()成员函数,实现多态性,应该在CGoods类及其派生类CClothes和CFood类中作何改动? 主函数以何种方式调用?
- Assistant
第一问:
#include <iostream> #include <string.h> using namespace std; class CGoods { protected: long no; char* p_name; double price; public: CGoods(long no_val, char* p_val, double price_val); CGoods(const CGoods& g); ~CGoods() { delete[] p_name; } void print() const; }; CGoods::CGoods(long no_val, char* p_val, double price_val) { no = no_val; p_name = new char[strlen(p_val)+1]; //注意:加上 '\0' 的大小 strcpy(p_name, p_val); price = price_val; } CGoods::CGoods(const CGoods& g) { no = g.no; p_name = new char[strlen(g.p_name)+1]; strcpy(p_name, g.p_name); price = g.price; } void CGoods::print() const { cout << "no: " << no << endl; cout << "p_name: " << p_name << endl; cout << "price: " << price << endl; } int main() { CGoods g1(1001, "book", 23.5); g1.print(); return 0; }输出结果:
no: 1001 p_name: book price: 23.5利用字符串分配动态内存的方式,对CGoods类进行初始化和复制构造,利用析构函数释放字符串分配的堆内存。使用 print() 成员函数输出数据成员的值。
- User
回答上述问题
- Assistant
第二问:
在 CGoods 类中添加一个商品总数 int count 数据成员,并增加一个 getCount() 成员函数,用于获取 count 的值。再在 CGoods 类外编写一个 getName() 友元函数,用于输出商品名称 p_name。
代码如下:
#include <iostream> #include <string.h> using namespace std; class CGoods { protected: long no; char* p_name; double price; static int count; //商品总数 public: CGoods(long no_val, char* p_val, double price_val); CGoods(const CGoods& g); ~CGoods() { delete[] p_name; } void print() const; int getCount() const { return count; } friend void getName(const CGoods& g); }; int CGoods::count = 0; CGoods::CGoods(long no_val, char* p_val, double price_val) { no = no_val; p_name = new char[strlen(p_val)+1]; strcpy(p_name, p_val); price = price_val; count++; //每次创建新对象count加1 } CGoods::CGoods(const CGoods& g) { no = g.no; p_name = new char[strlen(g.p_name)+1]; strcpy(p_name, g.p_name); price = g.price; count++; } void CGoods::print() const { cout << "no: " << no << endl; cout << "p_name: " << p_name << endl; cout << "price: " << price << endl; } void getName(const CGoods& g) { cout << "商品名称为:" << g.p_name << endl; } int main() { CGoods g1(1001, "book", 23.5); g1.print(); cout << "商品总数为:" << g1.getCount() << endl; getName(g1); return 0; } - User
回答上述问题
- Assistant
第三问:
为 CGoods 类定义小于运算符 ('<') 和不小于运算符 ('>=') 两个运算符重载函数。CGoods 类对象大小的比较是根据其商品价格 price 的值的大小来判断的。
代码如下:
#include <iostream> #include <string.h> using namespace std; class CGoods { protected: long no; char* p_name; double price; public: CGoods(long no_val, char* p_val, double price_val); CGoods(const CGoods& g); ~CGoods() { delete[] p_name; } void print() const; bool operator<(const CGoods& g) const; bool operator>=(const CGoods& g) const; }; CGoods::CGoods(long no_val, char* p_val, double price_val) { no = no_val; p_name = new char[strlen(p_val)+1]; strcpy(p_name, p_val); price = price_val; } CGoods::CGoods(const CGoods& g) { no = g.no; p_name = new char[strlen(g.p_name)+1]; strcpy(p_name, g.p_name); price = g.price; } void CGoods::print() const { cout << "no: " << no << endl; cout << "p_name: " << p_name << endl; cout << "price: " << price << endl; } bool CGoods::operator<(const CGoods& g) const { return price < g.price; } bool CGoods::operator>=(const CGoods& g) const { return price >= g.price; } int main() { CGoods g1(1001, "book", 23.5); CGoods g2(1002, "pen", 3.2); CGoods g3(1003, "phone", 5699); if (g1 < g2) { g1.print(); cout << " is cheaper than "; g2.print(); cout << endl; } if (g3 >= g2) { g3.print(); cout << " is more expensive than or equal to "; g2.print(); cout << endl; } return 0; }