c++ 标准命名空间以及一些基础

#include#include//标准命名空间(包含很多标准的定义)

//standard  在C++工程中,先写入下面一句话

using namespace std;

这样就可以简写很多事,如输出就可简写为:

cout << "输出一句话" << endl;

如果没有加入那句话,则必须写成:

std::cout << "输出一句话" << std::endl;

//命名空间类似于Java中包(归类)

C++中的LOG有如下,std::就可以访问std里定义的成员/结构体/类

//运算符重载

//std::cout << "this is c plus plus" << std::endl;

cout << "this is c plus plus" << endl;

//自定义命名空间  访问其中的变量,并访问自定义命名空间中的结构体

namespace NSP_A{

   int a = 9;

   struct Teacher{

      char name[20];

     int age;

};

   struct Student{

      char name[20];

        int age;

    };

}

namespace NSP_B{

   int a = 12;

   //命名空间嵌套

   namespace NSP_C{

    int c = 90;

 }

}

void main(){

   //运算符重载

   //std::cout << "this is c plus plus" << std::endl;

   cout << "this is c plus plus" << endl;

   //使用命名空间

   //::访问修饰符

   cout << NSP_A::a << endl;

   cout << NSP_B::a << endl;

   cout << NSP_B::NSP_C::c << endl;

   //使用命名空间中的结构体

   using NSP_A::Student;

   Student t;

   t.age = 90;

   system("pause");

}

C++中的类定义

#define PI 3.14

//圆

class MyCircle{

//属性(共用权限访问修饰符)

   private:

   double r;

   double s;

   public:

   void setR(double r){

     this->r = r;

}

//获取面积

double getS(){

return PI * r * r;

}

};

void main(){

   MyCircle c1;

   c1.setR(4);

   cout << "圆的面积:" << c1.getS() << endl;

   system("pause");

}

C++中的结构体 与C中基本相同

//结构体

struct MyTeacher{

   public:

   char name[20];

   int age;

   public:

   void say(){

   cout << this->age << "岁" << endl;

}

};

void main(){

   MyTeacher t1;

   t1.age = 10;

   t1.say();

   system("pause");

}

你可能感兴趣的:(c++ 标准命名空间以及一些基础)