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

c++基础

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

c++基础

c++基础

文章目录
    • 1.注释
      • 多行注释
      • 单行注释
    • 2.调库
    • 3. 入口函数
    • 4. 输入输出
    • 5. 命令行执行
      • 生成可执行文件:
      • linux
      • mac
      • 执行:
  • c++有三个重要的概念
  • 类
    • 6. c++类的架构
    • 7. 创建类对象
    • 8.命名空间
      • 定义
    • 9.继承
    • 10.多态
    • 11.内存地址空间
    • 12.堆空间与栈空间
    • 13.深拷贝与浅拷贝

1.注释 多行注释
 
单行注释
// 单行注释
2.调库

在当前目录下,则使用” “

在系统目录下,则使用<>

#include  //包含c中的输入输出库
#include  //包含c++中的输入输出,一般使用这个
#include "Human.h" //在当前目录下,则使用” “
3. 入口函数

在命令行执行的时候,argc表示参数的个数,argv数组中存放每个位置的参数

int main(int argc,char* argv[])
{

}
4. 输入输出

std 是一个命名空间,后面将讲解

endl表明换行

输出是<<,指向std::cout ,代表输出给这个类,这个类负责输出到屏幕上

输入是>>, 指向变量, 代表这个类捕捉输入送到一个变量上。

//输出
std::cout<<"hello world!"<>a;
5. 命令行执行

g++/clang++ 代表编译器

-std=c++11 选择c++标准

-g 可调试

-o 输出可执行文件

hello 可执行文件的名字

helloworld.cpp 源码的名字

生成可执行文件: linux
g++ -std=c++11 -g -o hello helloworld.cpp
mac
clang+ -std=c++11 -g -o hello helloworld.cpp
执行:

linux 和mac 一样

直接文件名即可

hello
c++有三个重要的概念
  1. 类

  2. 继承

  3. 多态(父亲儿子都有手,但儿子的手跟父亲的手是不一样的)

类 6. c++类的架构

头文件: monster.h

#include
class Monster
{
    public:
        //构造函数
        Monster(){
           std::cout<<"construct Monster..."<
        }
    
    private:
        char eyebrow[20];
        int eye;
        char nose[20];
        char mouth[20];
    public:
    
        void walk();
        int think(int p);
        void use(int q);
}; 

源文件:monster.cpp

#include
#include"monster.h"
#include"monster.cpp"
void Monster::walk()
{
    std::cout<<"Monster is walking!!"<
    std:cout<<"Monster is thinking "<
    eye=q;
}

已经有了:Monster.cpp 和 Monster.sh

Monster这个类算是完成

“.h和.cpp文件本质上是没有什么区别的,.h文件实现的功能是声明本文件或.cpp文件中需要被别人使用或知道的变量、类型、函数及宏定义等。.h文件就像是一个接口,具体的实现可以在.cpp中也可以在.h中。.cpp中的函数或变量即便是全局的,在没有其他.cpp文件使用的情况下,也可以不写在.h中。”

#include
#include "Monster.h" //引入.h即可
int main(int arge,char* argv[])
{
    Monster monster;
    monster.use(8);
}
7. 创建类对象

创建类对象,栈中创建:Human human; 文件执行结束,该对象自动销毁(执行析构函数)

#include
#include "Monster.h" //引入.h即可
int main(int arge,char* argv[])
{
    Monster monster;
    monster.use(8);
}

创建类对象,堆中创建:Human *p = new Human(); 当你不使用时,需要通过 delete 删除掉,必须显示释放(执行析构函数)

#include
#include "Monster.h" //引入.h即可
int main(int arge,char* argv[])
{
    Monster* monster=new Monster();
    monster->use(8);
    delete monster;
}
8.命名空间

防止与其他库产生冲突

命名空间 结尾没有分号哦

防止与其他库产生冲突

就是给一部分代码穿一层 衣服

需要 衣服::…使用

python 中已经实现了 比如numpy库 和torch库 都有相同的函数

import numpy as np

np.ones() np就是衣服

定义
#ifndef __HUMAN_H_  # 防止多次引入
#define __HUMAN_H_
namespace avdance
{
    class Monster
    {...};
}//namespace
#endif //__HUMAN_H__
namespace avdance
{
    方法实现
}//namespace
#endif //__HUMAN_H__
### 使用

命名空间::

```cpp
```cpp
#include
#include "Monster.h" //引入.h即可
int main(int arge,char* argv[])
{
    avdance::Monster* monster=new Monster();
    monster->use(8);
    delete monster;
}
9.继承

按照两个权限中最低的作继承,private就没法继承

airplance.h


#ifndef _AIRPLANE_H_
#define _AIRPLANE_H_
#include
namespcace avdance{
class AirPlane{
  public:
    Airplane(){
    wings=2;
    wheels=3;
    engines=1;
};
    ~Airplane(){};
  public:
    void setWings(int w);
    int getWings();
    void setWheels(int w);
    int getWheels();
    void setEgine(int e)
    int getEngine();
  public:
    virtual void fly();
  private:
    int wings;
    int wheels;
    int engine;
};
}//namespace
#endif//_AIRPLANE_H_
airplane.cpp


#include
#include"airplane.h"
namespace advance{
void Airplane::setWings(int w){
    wings=w;
}
int Airplane::getWings(){
    return wings;
}
int Airplane::getWheels(){
    return wheels;
}
void Airplane::setEngines(int e){
    engines=e;
}
int Airplane::getEngines(){
    return engines;
}
void Airplane::fly(){
    std::cout<<"fly..."< 
fighterplane.h


#ifdef _FIGHTERPLANE_
#define _FIGHTERPLANE_

#include 
#include "airplane.h"

namespace avdance{
    class Fighterplane: public Airplane{
        public:
            Fighterplane(){
                weapons=1;
}:
            ~Fighterplane():
        public:
            void setWeapons(int w);
            int getWeapons();
        private:
            int weapons
    
};
}//namespace

fighterplane.cpp


#include
#include "FighterPlane.h"
namespace avdance{
    void FighterPlane::setWeapons(int w){
       weapons=w;
}
     int FighterPlane::getWeapons(){
       return weapons;
}
}//namespace
test.cpp


#include
#include "fighterplance.h"

int main(int argc,char* argv[])
{
    avdance::Fighterplane fp;
    fp.setWings(5);
    fp.setWheels(3);
    fp.setWeapons(10);
    std::cout<<"FighterPlane:"< 
执行
clang++ -g -o derived airplance.cpp fighterplane.cpp test.cpp

申请孩子 分配既包括基类空间又包括子类空间

申请父亲 分配基类空间

跟python一样

10.多态

我们说 起飞! 无人机 战斗机 等都有不同的起飞方式

同样的名字 ,不同的实现

airplance.h


#ifndef _AIRPLANE_H_
#define _AIRPLANE_H_
#include
namespcace avdance{
class AirPlane{
  ......
  public:

    virtual void fly();
  ......
};
}//namespace
#endif//_AIRPLANE_H_
无人机


noneplane.h


#ifdef _NONEPLANE_
#define _NONEPLANE_

#include 
#include "airplane.h"

namespace avdance{
    class Noneplane: public Airplane{
        public:
            Noneplane(){
                weapons=1;
}:
            ~Noneplane():
        public:
            void setWeapons(int w);
            int getWeapons();
        public:
            virtual void fly(){
                std::cout<<"Noneplace fly ..."< 
noneplane.cpp


#include
#include "NonePlane.h"
namespace avdance{
    void NonePlane::setWeapons(int w){
       weapons=w;
}
     int NonePlane::getWeapons(){
       return weapons;
}
}//namespace
test2.cpp


#include
#include"noneplane.h"
int main(int argc,char* argv)
{
    avdance::airplane * ap=new noneplane();//真正指向的是无人机
    ap->fly();//"noneplane fly..."
    
    
}
11.内存地址空间

从应用的角度想,就当成一个超长的数组

以下是32位 (4G) 32位两个方面

  1. 地址最多32位,最多 2 32 2^{32} 232个地址,说明内存4GB

  2. 能处理32位数,-255-256,一个地址存4B

0x00000000-0x80000000 这一块是操作系统用的


一个地址存4B

12.堆空间与栈空间

堆空间是从低地址到高地址

栈空间是从高地址到低地址

留给双方很大缓冲

堆空间 new delete 数组释放 delete[]

栈空间:

  1. 先压入函数的参数

  2. 再压入函数的返回地址

  3. 再压入函数本地变量(临时变量,不需显示释放空间)

结束后,会按顺序退栈

13.深拷贝与浅拷贝

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

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

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