#include <iostream>usingnamespacestd;
//圆是一个类//圆有半径,周长和面积//通过圆的半径可以求出周长或面积class Yuan{
private:
constdouble PI = 3.14;
double r;//半径public:
void setR(double cr){
r = cr;
}
double getR(){
return r;
}
//得到面积double getCS(){
return PI * r *r;
}
//得到周长double getCC(){
return PI * 2 * r;
}
};
int main(int argc, char *argv[])
{
//定义一个圆
Yuan yuan ;
double cr = 0.0;
cout<< "please to r:"<<endl;
cin>>cr;//输入远的半径
yuan.setR(cr);
cout<<"ciler s is :"<<yuan.getCS()<<endl;
cout<<"ciler c is:"<<yuan.getCC()<<endl;
return0;
}
* 一个典型的错误
#include <iostream>usingnamespacestd;
class Test{
int a ;
//int b = a * a * a;//Error//解决:(在类中加入成员函数)int getB(){
b = a * a * a;
return b;
}
};
int main(){
cin>>Test.a;
cout<<b<<endl;//Error(b的值并没有进行计算(a*a*a),而是从b的内存地址取得值)return0;
}
* namespace<命名空间> 的使用
#include <iostream>#include <stdio.h>#include <stdlib.h>usingnamespacestd;
namespace A{
int a = 10;
}
namespace B{
int a = 50;
namespace C {
int a = 100;
typedefstruct Teacher{
int a;
}Teacher;
}
}
int main(int argc, char *argv[])
{
usingnamespace A;//A中的所有表示符都可以直接使用usingnamespace B;//B中的所有标识符都可以直接使用,但是C中的却不行.需要加[C::]//当命名空间中含有相同的标识符的时候需要加入命名空间的名称来使用cout<<"A::a="<<A::a<<endl;
cout<<"B::a="<<B::a<<endl;
cout<<"C::a="<<C::a<<endl;
/*Error
C::Teacher t = (C::Teacher *)malloc(sizeof(C::Teacher));
*/
C::Teacher * t = new C::Teacher;
t->a = 1500;
cout<<"Teacher::a="<<t->a<<endl;
delete t;
return0;
}
* C/C++的异同
>1.register(寄存器变量)
registerint a = 100;
int * p = &a;//C:Error C++:OK//C语言对于寄存器变量不允许做取地址操作,而C++可以//C++对于多次使用的变量会自动优化变成寄存器变量
>2.for循环
for (int i = 0;i < 1000;i++);//C:Error C++:OK//C语言不允许int i在for循环内部定义,而C++可以
>3.变量的重复声明
int a ;
int a = 100;
//C:OK C++:Error//C++不允许变量重复声明
>4.struct 类型增强
#include <iostream>#include <string.h>#include <ctype.h>usingnamespacestd;
struct Student{
private:
int age;
char name[64];
public:
void setAge(int a){age = a;}
void setName(char * str){strcpy(name,str);}
void show(){
cout<<"Name = "<<name<<" age = "<<age<<endl;
}
};
int main(int argc, char *argv[])
{
Student stu;//C:Error 在没有使用typedef 必须定义struct Student stu;
stu.setAge(18);
stu.setName("yzh");
stu.show();
return0;
}
//C++的struct 允许直接使用结构体的名称来定义变量//C++的struct 允许使用权限访问修饰符(public,protected,private)
*C++增加了类型检查
f(i){return10;}
g(){return ;}
void main(){
int a = 10;
f(a);//C++:Error
g(1,2,3,4,5);//C++:Errorreturn0;
}
//C:OK//C++:Error//C++增加了类型检查机制,强制要求函数的定义与使用规范化
int a = 10;
int b = 20;
(a<b ? a : b) = 50;//C:Error C++:OK
//C语言返回一个值,C++返回一个左值
//C++要想返回一个左值[1.非const常量 2.必须有自己的内存空间]
//C实现C++的三目运算符效果
*(a<b ? &a : &b) = 50;
* C/C++中的const
>C/C++中相同:
>const 修饰指针
constint * p ;//p指向的内存不允许被修改,但p可以更给指向int * const p;//p的指向不允许修改,但指向的内存可以修改constint * const p;//只读
>const做函数参数
typedefstruct Teacher{
int age;
}Teacher;
void funa(const Teacher * t){//t所指向的内存不允许修改
t->age = 10;//Error
t = NULL;//OK
}
void funb(Teacher * const t){//t的指向不允许修改
t = NULL;//Error
t->age = 100;//OK
}
void func(const Teacher * const t){//只读
t = NULL;//Error
t->age= 200;//Error
printf("%d",t->age);//OK
}
>C/C++中的异同:
>C中的cosnt常量(伪常量)
constint a = 10;//或int const a = 10;
printf("a = %d",a); //a = 10;int * p = NULL;
p = (int *)&a;
*p = 20;
printf("a = %d",a); //a = 20;//C语言中的常量可以通过取常量地址间接的修改常量的值,并非真正的常量
>C++中的const常量(真正常量)
constint num = 10;//C++将const常量放在一张由(key=value)组成形式的符号表中int * p = NULL;
p = (int *)# // C++将为num重新分配了一块内存
*p = 100;
cout<<"num = "<<num<<" &num = "<<&num<<endl;
cout<<"*p = "<<*p <<" p = "<<p<<endl;
* C++中const和define
#include <iostream>usingnamespacestd;
void funA(){
#define a 10// const int a = 20; //1.将#define 修改为cosnt //#undef a //2.通过undef解除定义,限定#define的作用域
}
void funB(){
cout<<"a = "<<a<<endl;
}
int main(int argc, char *argv[])
{
funB();
funA();
return0;
}
//#define 只是机械式的做替换,const则有明确的作用域,比#define更安全
* C++引用(Type &name = value)
>引用是C++的新特性,C不支持
int a = 10;
int &b = a;//b是a的引用[==>int * const p = &a ]int &c ;//Error (引用在定义时必须指明是谁的别名)int a = 10;
int &b = a;
cout<<"a = "<<a<<" b = "<<b<<endl;
cout<<"&a = "<<&a<<" &b = "<<&b<<endl;
//b是a的引用,b其实是a的别名,和a操作相同的一段内存
>在外部函数中交换两个变量的值
//1.指针void swap(int * a,int * b){
int temp = *a ; *a = *b;*b = temp;
}
//2.引用void swap(int &a,int &b){
int temp = a;a = b; b = temp;
}
>1.引用变量也占用内存大小
typedefstruct Teacher{
char str[64];//64int age;//4int &a;//4int &b;//4
}Teacher;
cout<<"sizeof(t) = "<<sizeof(Teacher)<<endl;//sizeof(Teacher) = 76
>2.C++编译器实现引用
int a = 10;
int &b = a;//==> int * const b = &a;//引用实质是一个无法修改指向的指针//引用做函数参数的实现void funA(int &b){
b = 30;
}
void funA(int * const b ){
*b = 30;
}
* 引用和函数的返回值
#include <iostream>usingnamespacestd;
int getA(){//返回a的副本int a = 10;
return a;
}
int& getB(){ //返回a自身int a = 10;//栈上分配内存会在函数结束时被系统回收,容易引起错误// static int a = 10;//解决1.加入static 2.提升为全局变量return a;
}
int* getC(){//返回a的引用int a = 10;栈上分配内存会在函数结束时被系统回收,容易引起错误// static int a = 10;//解决1.加入static 2.提升为全局变量return &a;
}
int main(int argc, char *argv[])
{
int a = getA();
int b = getB();
int &c = getB();
int * d = getC();
cout<<"A = "<<a<<endl;
cout<<"B = "<<b<<endl;//Error 栈上分配内存导致错误cout<<"C = "<<c<<endl;//Error 栈上分配内存导致错误cout<<"D = "<<*d<<endl;//Error 栈上分配内存导致错误return0;
}
* 引用可以当左值或者右值
#include <iostream>usingnamespacestd;
int A(){
staticint a = 10;
cout<<"A::a = "<<a<<endl;
return a;
}
int & B(){
staticint a = 10;
cout<<"B::a = "<<a<<endl;
return a;
}
int main(int argc, char *argv[])
{
int a = A();//OKint b = 0;
//A() = b;//Errorint c = B();//OKint d= 100;
B() = d;//OK
B(); //B::a的值被该变了return0;
}
//函数要当左值,必须返回一个引用