栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++基础(一)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++基础(一)

命名空间

#include 
#include 

using namespace std;

namespace A { //namespace + X {} 命名空间的书写格式,自定义的命名空间A的域
    int a;
    float b;
    void show(){
        cout<<"hello"<  //嵌套名字空间的一种方法
        int a;
        float b;
    }
}
    //为全局变量,在静态区,初始化了在.data区,没有初始化在.bss区,
    //const修饰在常量区。rodata端;函数在代码段txt段
    //全局变量调用就初始化
//    cpye_data_section();
//    clear_bss_section(); 将bss段为初始化的变量初始化为0

using namespace A;
//using A::a; // 如果程序中指向用到一个名字空间中的一个变量符的话,可以使用using 名字空间(域名)::变量名(函数名)
//using A::show;
int main() //一个进程运行,开辟4G空间
{
    cout< 

c++结构体

#include 

using namespace std;

struct Stu{ //C++中struct主要存储数据信息,用于存放不同类型数据
    int age;
    string name;//智能对象,自动增加字符串长度;字符指针不能自动增加长度
    int height;
    void action(){// 在C中只能在结构体外部,定义函数指针,对函数指针进行赋值
        cout<<"正在学习"<
//    Stu stu;
//    Stu stu{18, "zhangsan", 180};
      Stu stu = {18, "zhangsan", 180};
//    stu = {18,"zhangsan", 180};
    cout< 

c结构体

#include 

typedef void(*PFunc)(); //自定义的函数指针,类型与函数一致
struct Stu{
    int age;
    char* name;
    int height;
    PFunc action;
}; //通过函数指针调用结构体内元素

void WriteCode(){
    printf("正在写代码n");
}

int main()
{
    struct Stu stu = {18, "zhangsan", 180, WriteCode};

    stu.action();//调用stu的行为
    printf("Hello World!n");
    printf("%dn", stu.height);
    return 0;
}

c字符串

#include 

int main()
{
    //   /0越界问题
    char str1[] = {"hello"};  //编译器会自动在数组末尾加0;
    char str2[] = {'h', 'e', 'l', 'l', 'o'};
    char str3[] = "hello";
    printf("%dn", sizeof(str1));  // 6
    printf("%dn", sizeof(str2));  // 5
    printf("%dn", sizeof(str3));  // 6

    char* str4 = "C++";  //只能访问,不能修改

    return 0;
}

c++字符串

#include 

using std::cout;
using std::endl;
using std::string;

int main()
{
    string str = "hello"; //C++尾部不带0
    cout< 

宏函数

#include 

//宏函数,在预处理阶段,不参编译,更高效
#define Max(a, b) ((a)>(b)?(a):(b))

int main()
{
    int a=10;
    int b=11;
    printf("%dn", b++);
    printf("%dn", Max(a, b));
    return 0;
}


//容易出错,c++内联函数

内联函数

#include 

using namespace std;

inline void show() //调用频率大,且简短的函数,高效
{
    cout<<"hello "<
    show();
    cout << "Hello World!" << endl;
    return 0;
}


// 内联函数和宏函数--------空间换时间
// 宏函数-----增强代码的复用性,在预处理阶段会将使用宏函数的位置用宏展开
// 减少了函数栈操作等开销,因此可以 调高程序运行效率。
// 内联函数-----在编译时会在调用内敛函数的地方进行展开(直接调用,省了栈操作),
// 没有函数的栈操作
// inline对编译器只是建议,编译器会自动进行优化。
// 定义在类中的函数数默认定义为内联函数


// 预处理 编译 汇编 链接



常函数

#include 

using namespace std;

const int c = 30; //静态区的只读数据段

void test()
{
    const int a = 100;   // 直接赋值,放在常量表中
    const int* p = &a;     //因为a类型为const int,加了取地址符&a,此时相当于const* int a,因为指针也是取地址;
    *p = 300;
    cout<<"a="<
    int a = 10;
    static const int d = 40; //存储在静态区的只读数据段, 局部变量存在栈区
    const int b = 20;    //只读的变量,在栈区,如果是局部变量,代码块运行之后,变量释放
    int f = 30;
    cout << "Hello World!" << endl;

    cout<< &a < 

函数

#include 

using namespace std;

int show(int a=1, int b=2, int c=3) //默认值从右向左赋值
{                                   //函数在调用时,参数的赋值是从左向右一次进行赋值的
    return a+b+c;
}

int main()
{
    cout< 

函数重载

#include 

using namespace std;

void func(){
    cout<<"这是函数1"<
    cout<<"这是函数3"<
    cout<<"这是函数4"<
    cout<<"这是函数4"<   func();
    func(3.14f);
    func(1);
    func(3.14, 5);
    cout << "Hello World!" << endl;
    return 0;
}

引用

#include 

using namespace std;

void swap(int& c, int& d){
    int temp = c;
    c = d;
    d = temp;

}

int main()
{
    int a= 100;
    int *p = &a;
    cout<<*p<#include 

using namespace std;

void test1()
{
    // 在堆区开辟空间
    int *pa = (int*)malloc(sizeof(int)); // malloc分配的是void类型,并没有进行初始化
    cout<<*pa<
    int* pa = new int;
    cout<<*pa<
    test1();
    test2();
    cout << "Hello World!" << endl;
    return 0;
}

#include 

using namespace std;


class Person
{
    string name;
    int age;
    char sex;

public:

    Person() = default;
    Person(string _name, int _age, char _sex)
    {
        name = _name;
        age = _age;
        sex = _sex;
    }

    void walk(){
        cout<<"walk"<
        cout<<"eat"<
        // C++不允许临时对象被修改或者重新赋值,所以对临时对象的引用必须是const类型
        // 一个非const引用,只能引用与其类型完全相同的对象,或者是其派生类的对象
        name = _name;
    }

    string getName()
    {
        return name;
    }

    void showInfo(){
        cout<<"人物信息:"<
    Person p1;   // 栈区
    Person *p2 = new Person;
    Person p3("zhangsan", 18, 'M');   //字符串除了用string类型,就只能使用char类型数组了
    p3.setName("lisi");
    p3.showInfo();
    cout << "Hello World!" << endl;
    return 0;
}

内存对齐

#include 

using namespace std;

enum Sex
{
    female,
    male
};

// #pragma pack(1)
class Stu
{
private:
    string name;
    int age;
    Sex sex;  // 枚举类型,默认是整形为4个字节
    char ff;  // 加一个char,由于类中最大字节数为4,故占8个字节
public:
    void writeCode()
    {
        cout<<"happy happy to write code"<
        cout<<"happy to eat"<
        this->name = name; //this 本对象
    }
    string getName()
    {
        return this->name;
    }
    void setAge(int age)  //int 很小,不需要引用
    {
        this->age = age;
    }
    int getAge()
    {
        return this->age;
    }
    void setSex(Sex sex)
    {
        this->sex = sex;
    }
    Sex getSex()
    {
        return this->sex;
    }

    Stu(){}
    ~Stu(){}
};
// #pragma pack()

int main()
{
    Stu stu;
      // 占4个字节
    // 1、求出这个类中成员内存最大的那一个,以这个内存大小为基准,进行内存字节对齐
    // 2、如果是结构体或数组,结构体和数组是连续存储的,求每个元素中最大的空间,求到最长的那个元素为基准。
    stu.setName("zhangsan");  //常量,左值引用
    return 0;
}

常对象

#include 

using namespace std;

class Plane
{
private:
    int height;
    int width;
public:
    int outArea()const   // 在函数体前加const常函数,在函数前加const,返回值为只读类型
    {
        return this->width * this->height;
    }
    void setValue(int width = 1, int height = 1)
    {   // 函数重载不设定初始值
        this->width = width;     //this类中的属性
        this->height = height;
    }
};


bool compared(const Plane& p1, const Plane& p2) // c++中不允许临时对象被修改或重新赋值
{
    return p1.outArea()>p2.outArea() ? true: false;
}

int main()
{
    Plane p1;
    p1.setValue(2, 3);
    cout<
        cout<<"p1 is big"<
        cout<<"p2 is big"< 

析构函数

#include 

using namespace std;

class Stu
{
private:
    string name;
    int age;
public:
//    Stu()  //默认空构造
//    {
//        cout<<"wucan kong gouzao "<
        this->name = name;
        this->age = age;
        cout<<"I am "<name<<", "<age<
//    Stu stu1;   // 隐式调用在栈区,会调用无参构造,调用无参构造的对象一定不要加括号。
//    Stu stu2(); //这是一个函数声明,不是定义对象
//    Stu* stu = new Stu; // 这是在堆区的隐式调用,在调用无参构造时,可以加括号,也可以不加,一样的前提是手写了无参构造。
    cout << "*******************************************************" << endl;
//    Stu stu3("zhangsan", 18);  //显示调用
    Stu* stu4 = new Stu();  //显示调用

    return 0;
}

// 1、构造函数所存在的意义? 初始化类中的属性。
// 2、构造函数的调用时机:当类对象在内存被定义,会自动调用对应的构造函数
// 3、如果类中没有写构造函数,编译器会自动添加一个默认构造函数,
// 这个构造函数版本会根据对象调用方式不同而不同
// 如果在栈区定义的无参对象,那么编译器会自动调用空构造
// 如果实在堆区定义的无参对象,那么编译器会自动调用有参,且有默认值得构造
// 相当于类中的属性进行了初始化

析构函数

#include 

using namespace std;

class B
{
public:
    B()
    {
        cout<<"B的构造"<
        cout<<"B的析构"< B* b;
public:
    A()
    {
        b = new B;
        cout<<"A的构造"<
        cout<<"A的析构"<
    A a; //在栈区,出栈后自动调用析构函数
    A* pa = new A;
    delete pa;

    cout << "Hello World!" << endl;
    return 0;
}

初始化列表

#include 

using namespace std;

class A
{
private:
     const int a;
     static int b;
public:
     // 初始化列表:是构造函数中特有的语法,格式是在构造函数的参数列表后:只读变量名(所要的赋值)
     // 初始化列表的用途,主要用来初始化一些需要构造对象前就要初始化一些类属性
     // 初始化列表的调用时机,是先于构造函数的 ,因为const修饰的变量必须进行初始化
    A():a(10)
    {

        cout<<"A的构造"<
        cout<<"A的析构"<
        return a;
    }
};
// 静态变量的初始化格式:类外,main前进行初始化 格式:类型名+类域::静态变量名 = 值
int A::b = 10;
int main()
{
//    int A::b = 10;
    A a;
    cout< 

分文件编程

#ifndef STU_H
#define STU_H  // 如果没有则定义
//#pragma once

#include 
using namespace std;

class Stu
{
    string name;
    int age;
    static int score; //类内声明,所有对象共享
public:
    Stu() = default;
    Stu(string name, int age);
    ~Stu()=default;
    void showInfo();
    int getScore();
};

#endif // STU_H

#include "stu.h"

Stu::Stu(string name, int age)
{
    this->name = name;
    this->age = age;
    cout<<"multiply param"<
    cout<<"Student name: "<name<<"age: "<age<
    return score;
}
int Stu::score = 100;

#include 
#include "stu.h"

using namespace std;

int main()
{
    Stu stu1;
    Stu stu2("zhangsan", 18);
    stu1.showInfo();
    cout<<"--------------------------------"< 

拷贝构造

#include 
#include 

using namespace std;

int main()
{
    Stu stu1("zhangsan", 18);
    Stu stu2 = stu1;    // 拷贝构造
    stu2.showInfo();
    Stu stu3(stu2);   // 直接进行拷贝赋值的拷贝为浅拷贝
    // 三次拷贝,拷贝对象都是指向同一片内存,
    // 当程序结束时,调用三次析构,多次释放空间
    cout << "Hello World!" << endl;
    return 0;
}




转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1012864.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号