由重载学习写C++(1)

  1. 重载双目运算符
  2. #include <iostream>
  3. using namespace std;
  4. class String
  5. {public:
  6. String()///定义默认构造函数
  7. {
  8. p=NULL;
  9. }
  10. String(char *str);//声明构造函数
  11. void display();
  12. private:
  13. char *p;//字符型指针,用于指向字符串                                                                  
  14. };
  15. String::String(char *str)//定义构造函数
  16. {
  17. p=str;
  18. }
  19. void String::display()
  20. {
  21. cout<<p;
  22. }
  23. int main()
  24. {
  25. String str1("Hello"),str2("Book");//定义对象
  26. str1.display();//调用公用成员函数
  27. cout<<endl;
  28. str2.display();
  29. cout<<edl;
  30. return 0;
  31. }                                                                                                 
  32. 先编写出最简单的程序框架。有了这个基础后,再增加其他必要的内容。下一节增加对运算符重载的部分
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值