C++基础知识点(2)关于输入输出,结构体,类的基础知识

一.基本格式

头文件#include<iostream>

using namespace std;

int main

{



}

二.输入输出

1.cout输出

#include <iostream>
using namespace std;
int main()
{
    int a=123;
    double b=3.13;
    const char *p="hello";
    cout<<"a="<<a<<"b="<<b<<"p="<<p<<"\n";
    while(1);
}

运行

a=123b=3.13p=hello

2.cin输入

#include <iostream>
using namespace std;
int main()
{
    int a=123;
    double b=3.13;
    char buf[10];
    cin>>a>>b>>buf;
    cout<<"a="<<a<<"b="<<b<<"buf="<<buf<<"\n";
    return 0;
}

键盘输入2 0.1 cxy
输出a=2b=0.1buf=cxy


三.结构体定义和类

1.结构体

(1)先构造后定义

例如

struct student
{
    int number;
    char name[5];
};









int main()
{
    struct student stu1;
    struct student stu2;
}

(2)构造的同时定义

struct student
{
    int number;
    char name[5];
}stu1,stu2;

(3)构造的同时进行定义,省略类型名,不能在定义新的变量了

struct 
{
    int number;
    char name[5];
}stu1,stu2;

(4)typrdef改名

typedef struct student
{
    int number;
    char name[5];
}stu;

以后的数据类型变成stu不再是student

c:结构体,数据可以随意访问,方法的指向也可能发生变化

因此c++我们提出了更多功能的类

2.类

c++

class demo{
					
					public:     //共有权限
					
					private:    //私有的
					
					protected:   //保护的
				};

c++的类可以在内部定义函数题了

并且分为公共和私有的部分,公共可以调用,私有却不行,因此也避免了c语言里一些结构体里面的数据可以随意访问的缺点

四.构造函数
      构造函数:类对象生成自动调用的函数
        普通构造{
            默认普通构造,没有参数和返回值
        }
        拷贝构造{
            浅拷贝
            深拷贝
        }
        移动构造{
            有临时对象产生时使用
        }

#include <iostream>
#include <cstring>
using namespace std;
class arr
{
    public:
        //普通构造
        arr(char *p=nullptr)
        {
            cout<<"原-normal"<<endl;
            str=new char[strlen(p)+1];
            strcpy(str,p);
        }
        //拷贝构造  浅
        /*    arr(const arr &obj)
              {
              cout<<"浅-cpy"<<endl;
              str=obj.str;
              }
              */
        //拷贝构造 深
        arr(const arr &obj)
        {
            cout<<"深-cpy"<<endl;
            str=new char[strlen(obj.str)+1];
            strcpy(str,obj.str);
        }
        //移动构造
        arr( arr &&obj)
        {
            cout<<"移动构造"<<endl;
            str=obj.str;
            obj.str=nullptr;
        }
        //获取返回值
        char*getval()
        {
            return str;
        }
        //设置值
        void setval(const int id,const char data)
        {
            str[id]=data;
        }
    private:
        char*str;
        int len;
};
int main()
{
    char buf[10]="world";
    arr A="hello";//普通构造
    //arr B=A;//浅拷贝
    //   arr C=A;//深拷贝
    arr D=buf;
    //A.setval(0,'a');
    cout<<A.getval()<<endl;
    //cout<<B.getval()<<endl;
    //   cout<<C.getval()<<endl;
    cout<<D.getval()<<endl;
    return 0;
}

tips:浅拷贝和深拷贝只能由一个存在,不然会存在歧义

        移动拷贝必须由普通构造才能实现

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞赴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值