当前位置: 首页 > news >正文

C++学习day4

思维导图:

练习:

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

class mydouble{
private:

    int a;
    int b;
public:

    mydouble(int a,int b):a(a),b(b){};
    void show()
    {
        cout << a << "." << abs(b) << endl;
    }
    mydouble operator+(const mydouble& r)
    {
        mydouble AB(0,0);
        AB.a= a + r.a;
        AB.b= b + r.b;                                       
        return AB;
    }

    mydouble operator-(const mydouble& r)
    {
        mydouble AB2(0,0);
        AB2.a = a - r.a;
        AB2.b = b - r.b;
        return AB2;
    }

    mydouble operator*(const mydouble& r)
    {
        mydouble AB3(0,0);
        AB3.a = a * r.a;
        AB3.b = b * r.b;
        return AB3;
    }


};

int main(int argc,const char** argv){
    mydouble A(3,14);
    A.show();
    mydouble B(1,23);
    B.show();

    mydouble AB=A+B;
    printf("A+B的值为:");
    AB.show();

    mydouble AB2 = A-B;
    printf("A-B的值为:");
    AB2.show();

    mydouble AB3 = A*B;
    printf("A*B的值为:");
    AB3.show();

}
                                                             

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>

using namespace std;

class myOut{
private:

public:
    void operator<<(int a)
    {
        printf("%d\n",a);
        cout<<a<<endl;
    }
    void operator<<(double a )
    {
        printf("%f\n",a);
        cout<<a<<endl;
    }
    void operator<<(const char* a)
    {
        printf("%s\n",a);
        cout<<a<<endl;                                          
    }
    void operator<<(ostream&(*endl)(ostream&))
    {
        printf("\n");
    }
};
/*ostream& operator<<(ostream& out,const myOut& r)
{
    out<<r;
*/
int main(int argc,const char** argv){

    myOut out;
    out<<1;
    out<<3.14;
    out<<"hello";
    out<<endl;
    return 0;
}

相关文章:

  • 基于大数据分析的门户信息推荐系统
  • 《STL 六大组件之容器篇:简单了解 list》
  • 国家天文台携手阿里云,发布国际首个太阳大模型“金乌”
  • 用Python实现TCP代理
  • Java数据类型与数据库类型映射技术文档
  • 基础框架系列分享:一个通用的Excel报表生成管理框架
  • MQTT 服务器(emqx)搭建及使用(一)
  • [图论]拓扑排序
  • 【JavaEE】MyBatis - Plus
  • deepseek v3-0324 化学键线式Canvas编辑器设计
  • 线程池自顶向下
  • 关于 @Autowired 和 @Value 使用 private 字段的警告问题分析与解决方案
  • # C++初阶——内存管理
  • 【mysql】日志:binLog、redoLog和undoLog
  • openwebui和keycloak集成,使用keycloak的用户名和密码登录
  • Ubuntu 安全限制遭突破:攻击者可利用内核漏洞提权
  • 如何使用AI去水印(ChatGPT去除图片水印)
  • Proxmox pct 部署debian
  • Elasticsearch安全加固指南:启用登录认证与SSL加密
  • Linux服务器组建与管理
  • 白玉兰奖征片综述丨动画的IP生命力
  • 京东CEO许冉:外卖日单量接近2000万单,看到外卖对平台拉动和转化效应
  • 排污染黑海水后用沙土覆盖黑泥?汕尾环保部门:非欲盖弥彰
  • 高波︱忆陈昊:在中年之前离去
  • 从“求生”到“生活”:医保纳入创新药让梗阻性肥厚型心肌病患者重拾生活掌控权
  • 他站在当代思想的地平线上,眺望浪漫主义的余晖