对cout << 重载的源代码

本文介绍了一个C++程序示例,展示了如何定义一个包含构造函数、析构函数、拷贝构造函数及赋值运算符重载的简单类。此外,还演示了如何重载类型转换运算符和函数调用运算符,以及如何自定义输出流操作符以实现类实例的输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;

 
class CA
{
public:
 CA(int i) : m_i(i)
 {
  cout << "CA@" << this << " Constructor(" << i << ")" << endl;
 }
 
 CA(const CA& a) : m_i(a.m_i)
 {
  cout << "CA@" << this << " CopyConstructor(" << &a << ")" << endl;
 }
 
 CA& operator = (const CA& a)
 {
  m_i = a.m_i;
  cout << "CA@" << this << " Operator=(" << &a << ")" << endl;
 }
 
 ~CA()
 {
  cout << "CA@" << this << " Destructor()" << endl;
 }
 
 int GetValue() const
 {
  return m_i;
 }
 
 bool operator()()
 {
  return (m_i == 0) ? false : true;
 }
 
 string operator() (const CA& a)
 {
  return (m_i == 0) ? "false" : "true";
 }
 
 operator string ()
 {
  return (m_i == 0) ? "convert to false" : "convert to true";
 }
       
protected:
 CA()
 {
  cout << "CA@" << this << " Constructor()" << endl;
 }
 
protected:
 int m_i;
};
 
ostream& operator << (ostream &o, const CA& a)
{
 return (o << "CA@" << &a << " Val=" << a.GetValue());
}
 
ostream& MyManipulator(ostream& o)
{
 return (o << "|myend|");
}
 
class CMyManipulator
{
public:
 CMyManipulator():m_i(0)
 {
 
 }
 
 CMyManipulator(int i):m_i(i)
 {
 
 }
 
 ~CMyManipulator()
 {
 
 }
 
 int GetValue() const
 {
  return m_i;
 }
 
protected:
 int m_i;
};
 
ostream& operator <<(ostream &o, const CMyManipulator& m)
{
 for (int i=0; i<m.GetValue(); ++i)
 {
  o << "|myend" << i << "|";
 }
 
 return o;
}
 
int main()
{
 CA a(0);
 cout << a << endl;
 cout << a() << endl;
 cout << a(a) << endl;
 cout << string(a) << endl;
 
 cout << "some string" << MyManipulator << endl;
 cout << "some string" << CMyManipulator(3) << endl;
 
 return 0;
}
#include <iostream> // 输入输出流库,用于cin/cout操作 #include <string> // 字符串处理库 #include <cctype> // 字符处理函数库(虽然本例未直接使用,但通常用于输入验证) using namespace std; // 使用标准命名空间,避免重复写std:: // 课题 - 表示单个课题的实体 class Topic { public: string title; // 课题标题(字符串型) bool isSelected; // 课题是否已被选择(布尔型) string selectedBy; // 选择该课题的学生姓名(字符串型) // 构造函数 - 创建课题对象时自动调用 Topic(string t) : title(t), isSelected(false), selectedBy("") {} // 参数t: 传入的课题标题 // : 后是成员初始化列表 // title(t) - 将成员title初始化为传入的t // isSelected(false) - 初始状态设为未选择 // selectedBy("") - 初始选择者设为空字符串 }; // 定义结束 // 课题管理系统 - 管理课题集合 class TopicManager { private: Topic** topics; // 指向课题指针数组的指针(动态二维数组) int capacity; // 当前数组的容量(可容纳的最大课题数) int size; // 当前实际存储的课题数量 // 调整数组容量的私有方法 void resize(int newCapacity) { // 创建新容量的指针数组 Topic** newTopics = new Topic*[newCapacity]; // 将原数组内容复制到新数组 for (int i = 0; i < size; ++i) { newTopics[i] = topics[i]; // 复制指针(浅拷贝) } // 释放原数组内存 delete[] topics; // 先删除指针数组 topics = newTopics; // 将topics指向新数组 capacity = newCapacity; // 更新容量值 } public: // 构造函数 - 初始化管理系统 TopicManager() : topics(nullptr), capacity(0), size(0) {} // 初始化列表: // topics(nullptr) - 初始化为空指针 // capacity(0) - 初始容量为0 // size(0) - 初始课题数为0 // 析构函数 - 对象销毁时自动调用,释放内存 ~TopicManager() { // 遍历所有课题对象,释放每个课题的内存 for (int i = 0; i < size; ++i) { delete topics[i]; // 删除每个课题对象 } delete[] topics; // 删除课题指针数组 } // 增加课题的公共方法 void addTopic(string title) { // 检查是否需要扩容 if (size >= capacity) { // 计算新容量:初始为2,之后倍增 int newCapacity = (capacity == 0) ? 2 : capacity * 2; resize(newCapacity); // 调用扩容方法 } // 在数组末尾创建新课题 topics[size] = new Topic(title); // 动态分配内存 size++; // 课题数量增加 cout << "课题已添加: " << title << endl; // 操作反馈 } // 删除课题的公共方法 void deleteTopic(int index) { // 检查索引有效性 if (index >= 0 && index < size) { cout << "课题已删除: " << topics[index]->title << endl; // 反馈 delete topics[index]; // 释放该课题的内存 // 移动后续元素填补空缺 for (int i = index; i < size - 1; ++i) { topics[i] = topics[i + 1]; // 指针前移 } size--; // 课题数量减少 } else { cout << "无效的课题索引。" << endl; // 错误提示 } } // 修改课题的公共方法 void modifyTopic(int index, string newTitle) { // 检查索引有效性 if (index >= 0 && index < size) { topics[index]->title = newTitle; // 更新标题 cout << "课题已修改: " << newTitle << endl; // 反馈 } else { cout << "无效的课题索引。" << endl; // 错误提示 } } // 显示所有课题的公共方法 void displayTopics() { cout << "当前课题列表:" << endl; // 标题 // 遍历所有课题 for (int i = 0; i < size; ++i) { // 输出序号和标题 cout << i + 1 << ". " << topics[i]->title; // 如果课题已被选择,显示选择者信息 if (topics[i]->isSelected) { cout << " (已选择 by " << topics[i]->selectedBy << ")"; } cout << endl; // 换行 } } // 选择课题的公共方法 void selectTopic(int index, string studentName) { // 检查索引有效性 if (index >= 0 && index < size) { // 检查课题是否已被选择 if (!topics[index]->isSelected) { topics[index]->isSelected = true; // 标记为已选择 topics[index]->selectedBy = studentName; // 记录选择者 cout << "你已选择课题: " << topics[index]->title << endl; // 反馈 } else { // 课题已被选择时显示选择者信息 cout << "该课题已被选择 by " << topics[index]->selectedBy << endl; } } else { cout << "无效的课题索引。" << endl; // 错误提示 } } // 查看已选课题的公共方法 void viewSelectedTopics() { cout << "已选课题列表:" << endl; // 标题 // 遍历所有课题 for (int i = 0; i < size; ++i) { // 只显示已被选择的课题 if (topics[i]->isSelected) { // 输出课题标题和选择者 cout << topics[i]->title << " by " << topics[i]->selectedBy << endl; } } } }; // 菜单显示函数 void menu() { // 输出菜单选项 cout << "Menu:" << endl; cout << "1. 添加课题" << endl; cout << "2. 删除课题" << endl; cout << "3. 修改课题" << endl; cout << "4. 显示所有课题" << endl; cout << "5. 选择课题" << endl; cout << "6. 查看已选课题" << endl; cout << "7. 退出系统" << endl; } // 获取有效整数输入的函数 int getValidIntInput() { int value; // 存储输入值的变量 // 循环直到获得有效输入 while (!(cin >> value)) { cout << "输入无效,请重新输入: "; // 错误提示 cin.clear(); // 清除错误状态 cin.ignore(10000, '\n'); // 忽略缓冲区中的错误输入 } return value; // 返回有效整数 } // 主函数 - 程序入口点 int main() { TopicManager manager; // 创建课题管理器对象 int choice; // 存储用户选择的变量 // 主循环 do { menu(); // 显示菜单 cout << "请选择操作: "; choice = getValidIntInput(); // 获取有效整数输入 cin.ignore(); // 忽略换行符,防止影响后续输入 // 根据用户选择执行操作 switch (choice) { case 1: { // 添加课题 string title; cout << "请输入课题名称: "; getline(cin, title); // 读取整行输入 manager.addTopic(title); // 调用添加方法 break; } case 2: { // 删除课题 cout << "请输入要删除的课题索引: "; int index = getValidIntInput(); // 获取索引 manager.deleteTopic(index - 1); // 调用删除方法(索引-1转换为0-based) break; } case 3: { // 修改课题 cout << "请输入要修改的课题索引: "; int index = getValidIntInput(); // 获取索引 cin.ignore(); // 清除换行符 string newTitle; cout << "请输入新的课题名称: "; getline(cin, newTitle); // 读取新标题 manager.modifyTopic(index - 1, newTitle); // 调用修改方法 break; } case 4: // 显示所有课题 manager.displayTopics(); // 调用显示方法 break; case 5: { // 选择课题 cout << "请输入要选择的课题索引: "; int index = getValidIntInput(); // 获取索引 cin.ignore(); // 清除换行符 string studentName; cout << "请输入你的名字: "; getline(cin, studentName); // 读取学生姓名 manager.selectTopic(index - 1, studentName); // 调用选择方法 break; } case 6: // 查看已选课题 manager.viewSelectedTopics(); // 调用查看方法 break; case 7: // 退出系统 cout << "退出系统。" << endl; // 退出提示 break; default: // 无效选择处理 cout << "无效的选择。" << endl; break; } cout << endl; // 添加空行,使界面更清晰 } while (choice != 7); // 当选择7时退出循环 return 0; // 程序正常结束 }
06-17
创建源文件"学号+实验10-1".cpp,完成程序的编辑、编译、连接与运行。 编写程序声明Point,在中声明整型的私有成员变量x、y,声明成员函数Point& operator++( );Point operator++( int);以实现对Point重载“++”(自增)运算符,声明成员函数Point& operator--( );Point operator--( int);以实现对Point重载“--”(自减)运算符,实现对坐标值的改变。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬ 在主函数中完成充分测试! 输入 输出 1 1 a的值为:1 , 1 after a++,a的值为:2 , 2 after ++a,a的值为:3 , 3 after a--,a的值为:2 , 2 after --a,a的值为:1 , 1 #include<iostream> using namespace std; class Point { private: int x,y; public: Point(int nx=0,int ny=0):x(nx),y(ny){ } Point& operator++( ); Point operator++(int); Point& operator--( ); Point operator--(int); int getx(){return x;} int gety(){return y;} }; ........ int main() { int m,n; cin>>m>>n; Point a(m,n); cout<<"a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; a++; cout<<"after a++,a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; ++a; cout<<"after ++a,a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; a--; cout<<"after a--,a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; --a; cout<<"after --a,a的值为:"<<a.getx()<<" , "<<a.gety()<<endl; return 0; }
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值