类的封装
#include<iostream>
using namespace std;
class Persion
{
public:
char p_name[20];
int p_age;
void PersonEat()
{
cout << p_name << "在吃饭" << endl;
}
private:
};
struct Dog
{
char d_name[20];
int d_age;
void DogEat()
{
cout << d_name << "在吃狗粮" << endl;
}
};
void test01()
{
Persion p;
strcpy_s(p.p_name, 20, "老王");
Dog d;
strcpy_s(d.d_name, 20, "旺财");
p.PersonEat();
d.DogEat();
}
int main()
{
test01();
system("pause");
return 0;
}
私有属性
#include<iostream>
using namespace std;
#include<string>
class Persion
{
public:
void set_name(string name)
{
p_name = name;
}
string get_name()
{
return p_name;
}
int get_age()
{
return p_age;
}
private:
string p_name;
int p_age = 30;
};
void test01()
{
Persion p;
p.set_name("老王");
cout << p.get_name() << endl;
cout << p.get_age() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}