c++部分知识

之前有c语言基础,学起来c++很快,以下为c++的一些基本知识点,代码很简单,全部运行通过,以后会补上这些代码运行的内部原理。

  1. auto用法
#include<iostream>
int main(void)
{
    int number = 5;
    auto result = number;
    std::cout<<typeid(result).name()<<std::endl;
    return 0;
}
  1. static用法
#include<iostream>
static count = 0;
void add(){count++}

int main()
{   
    for(int i=0;i<5;i++)
        add();
    cout<<count<<endl;
    return 0;
}
  1. extern
///////////////////////support.cpp/////////////////////////////
#include<iostream>
void support()
{
    std::cout<<"support.cpp is running"<<std::endl;
}
////////////////////////main.cpp////////////////////////
#include<iostream>
extern void support();
int main()
{
    support();
    return 0;
}

c语言结构体

#include<stdio.h>
typedef struct{
    char name[10];
    char sexy[10];
    int years;
}Student,*STU;
void show_information(STU stu)
{
    printf("my name is %s  sexy is %s   years is %d",stu->name,stu->sexy,stu->years);
}
void main()
{
    Student stu;
    scanf("%s%s%d",&stu.name,&stu.sexy,&stu.years);
    show_information(&stu);
}

c++类

  1 #include<iostream>
  2 using namespace std;
  3 class Student{
  4   public:
  5         char name[10];
  6         char sexy[10];
  7         int years;
  8   public:
  9         void show_information(){
 10         cout<<"my name is :"<<this->name
 11                 <<"  sexy is : "<<this->sexy
 12                 <<"  years is : "<<this->years<<endl;
 13         }
 14 };
 15 
 16 int main()
 17 {
 18         Student stu;
 29         cout<<"please input name sexy and years"<<endl;
 20         cin>>stu.name>>stu.sexy>>stu.years;
 21         stu.show_information();
 22         return 0;
 23 }

c++继承类

  1 #include<iostream>
  2 using namespace std;
  3 //base class
  4 class Rectage{
  5     protected:
  6         int mLongth;
  7         int mWidth;
  8 };  
  9 //derived class
 10 class Cube:protected Rectage{
 11     private:
 12         int mHighth; 
 13     public:
 14         void set(int longth,int width,int highth){
 15         this->mLongth=longth;
 16         this->mWidth=width;
 17         this->mHighth=highth;
 18     }
 19     public:
 20     void getArea(){
 21     cout<<mLongth*mWidth*mHighth<<endl;
 22     }
 23 };  
 24 int main()
 25 {
 26     Cube cube;
 27     int longth,widhth,highth;
 28     cube.set(1,2,3);
 29     cube.getArea();
 30     cube.set(2,3,4);
 31     cube.getArea();
 32     return 0;
 33 }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值