c向c++过度

c向c++过渡

1c++基础

C++特点是一门面向对象的语言

面向对象的3个特点继承,封装,多态。

C++文件结尾以.cpp结尾或者以.cc结尾

编译是以g++加文件进行编译

C++继承了c的头文件

demo:

#include<cstdio>

#include<cstring>

#include<cstdlib>等等

2c++空间命名

关键字namespace:

namespace +你要命名的名字

{

//空间命名的作用域

//变量名,函数名,类名

}

空间命名的意义的程序大型化会有命名冲突。需要导入数据

3c++的导入

关键字为using

//头文件

#include<iostream>

#include<cstdio>

//必须写

using namespace std;

namespace A

{

    int a=100;

    void showinfo(){

        cout<<"hello"<<endl;

    }

}

//创建一个main函数

int main()

{

    cout<<A::a << endl;//数字

    A::showinfo();//函数字符串

   

   

    return 0;

}

4.C++中结构体

结构体用来表述一个对象

//头文件

#include<iostream>

//必须写

using namespace std;

class Stu

{

    public:

        string name;

        int age;

        void write_code()

        {

            cout<<"正在努力学习"<<endl;

        }

};

int main()

{

    Stu stu={"nba",18};

    cout<<stu.name<<endl;

    cout<<stu.age<<endl;

    stu.write_code();

    return 0;

}

  1. c++中的string

C++中就有了专属字符串类型string,它是C++的内置类型

#include <iostream>

using namespace std;

int main()

{

    //1.定义一个string 对象

    string name = "gaowanxi";

    //char arr[1024] = "I love you";//这样的C风格的字符串表式形式过于的浪费内存空间。

    cout << name << endl;

    //2. << 就是一个运算符重载函数 operator<<(形参列表)

    //cout << name.at(100) << endl; //这样的方式是有边界检查的。

    cout << name[100] << endl;//这样的方式是没有边界检查的。

    string name2 = ",lisi";

    cout << name + name2 << endl; //两个字符串的连接,返回值是一个新的字符串。

    //3. (name +=name2) 是调用的+=运算符重载函数。拼接两个字符串。

    cout << (name += name2) << endl;

    //4.cout << 可以输出所有的内置类型常量值无需C中的占位符。因为他是一个cout对象调用的 <<运符符重载函数;

    cout << "hello" << 100 << 3.14 << endl;

    //4.name.append(name2);  append()拼接两个字符串。

    name += name2;

    cout << name << endl;

    return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值