00_c++中的类创建、继承、静态属性、静态方法、友元关系

#include<iostream>
#include<string>

class first_class
{
public:
	first_class();
	~first_class();
	void say_hello();
	static int getCount();//静态方法

protected:
	int count = 0;

private:
	friend class third_class; //友元关系声明
	static int test_count; //静态属性
	
};

//继承类声明
class second_class : public first_class
{
public:
	second_class();
	~second_class();
};
1.public继承:基类public成员,protected成员,private成员的访问属性在派生类中分别变成:public, protected, private

2.protected继承:基类public成员,protected成员,private成员的访问属性在派生类中分别变成:protected, protected, private
只能被派生类和友元关系引用

3.private继承:基类public成员,protected成员,private成员的访问属性在派生类中分别变成:private, private, private
只能类内和友元关系使用

class third_class
{
public:
	void printf_count(first_class * the_class);
};

int first_class::test_count = 0;

first_class::first_class()
{
	std::cout << "the first class open \n" ;

	test_count++;

}

first_class::~first_class()
{
	std::cout << "the first class close \n";
}

void first_class::say_hello()
{
	std::cout << "hello c++. \n" ;
}

int first_class::getCount()
{
	return test_count;
}

second_class::second_class()
{
	std::cout << "the second class open \n";
}

second_class::~second_class()
{
	std::cout << "the second class close \n";
}

void third_class::printf_count(first_class * the_class)
{
	std::cout << "the count is " << the_class->test_count << "\n";
}

int main()
{
	first_class class_1;
	second_class class_2;
	third_class class_3;

	class_1.say_hello();

	std::cout << first_class::getCount() << "\n";

	class_3.printf_count(&class_1);

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值