C++的命名空间

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();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值