#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;
}
00_c++中的类创建、继承、静态属性、静态方法、友元关系
最新推荐文章于 2023-01-07 14:38:56 发布