1.standard 标准命名空间 ,如果使用cout<<"sss"<<endl;必须加上using namespce standard
using namespace std;
//命名空间类类似于Java中包(归类)
2.自定义命名空间
#include <stdlib.h>
#include<iostream>
using namespace std;
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 = 10;
//命名空间嵌套
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 s1;
s1.age = 90;
getchar();
}
3. C++中的类
#include <stdlib.h>
#include<iostream>
using namespace std;
#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(5);
cout << "圆的面积是:" << c1.getS() << endl;
getchar();
}
4.结构体
#include <stdlib.h>
#include<iostream>
using namespace std;
struct MyTeacher {
public:
char name[20];
int age;
public :
void say() {
cout << this->age << "岁" << endl;
}
};
void main() {
MyTeacher t1;
t1.age = 20;
t1.say();
getchar();
}
5.布尔类型
void main() {
// bool isSingle = true;
bool isSingle = 17;
//false -17
if (isSingle)
{
cout << "单身" << endl;
cout << sizeof(bool) << endl;
}
else {
cout << "有对象" << endl;
}
int a = 10, b = 20;
((a > b) ? a : b) = 30;
cout << b << endl;
getchar();
}