1舆绎、簡單的C++程序
輸出helloworld
2鲤脏、介紹面向過程和面向?qū)ο?/p>
image.png
3、C++中struct類型的加強(qiáng)
//沒有對屬性添加訪問修飾符的話猎醇,默認(rèn)公有
//c語言中結(jié)構(gòu)體中不能定義函數(shù),只能使用函數(shù)指針
struct student{
char name[20];
int score;
};
//沒有對屬性添加訪問修飾符的話硫嘶,默認(rèn)私有
class stu1{
char name[20];
int score;
};
4、C++中所有的變量和函數(shù)都必須有類型
對于如下的代碼:c是不報錯的沦疾,c++報錯
f(i){
printf("i=%d\n",i);
}
g()
{
return 5;
}
int main()
{
f(10);
printf("g() = %d\n", g(1, 2, 3, 4, 5));
return 0;
}
結(jié)論:
在C語言中
int f();表示返回值為int曹鸠,接受任意參數(shù)的函數(shù)
int f(void)斥铺;表示返回值為int的無參函數(shù)
在C++中
int f( )和int f(void)具有相同的意義彻桃,都表示返回值為int的無參函數(shù)
5晾蜘、c++中新增bool類型
6邻眷、引用
image.png
普通引用
引用做函數(shù)參數(shù)
復(fù)雜數(shù)據(jù)做函數(shù)參數(shù)
函數(shù)的返回值是引用
#include <iostream>
using namespace std;
int func1(){
int a=10;
return a;
}
int &func2(){
int a=10;
return a;
}
int *func3(){
int a=10;
return &a;
}
int main(){
int a=func1();//10
int b=func2();/10
int &c=func2();
int *p=func3();
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<*p<<endl;
getchar();
return 0;
}
image.png