之前有c语言基础,学起来c++很快,以下为c++的一些基本知识点,代码很简单,全部运行通过,以后会补上这些代码运行的内部原理。
- auto用法
#include<iostream>
int main(void)
{
int number = 5;
auto result = number;
std::cout<<typeid(result).name()<<std::endl;
return 0;
}
- static用法
#include<iostream>
static count = 0;
void add(){count++}
int main()
{
for(int i=0;i<5;i++)
add();
cout<<count<<endl;
return 0;
}
- 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 }
1万+

被折叠的 条评论
为什么被折叠?



