带你学习c++系列(一)我把自己写成了一个类
作者:互联网
适合小白快速理解与上手,我把自己写成了一个类带你理解C++的类。
运行结果:
完整代码:
Visual_eagle.h
#ifndef __VISUAL_EAGLE_H__
#define __VISUAL_EAGLE_H__
#include<iostream>
#include<ctime>
//使用命名空间
namespace people
{
class Visual_eagle
{
public:
Visual_eagle();//构造函数
~Visual_eagle();//析构函数
void Call_me_to_eat(std::string food_name, int dist, int time);//叫我吃饭,返回值是吃啥,输入值是你想吃啥,距离有多远,预计吃一顿饭花多久时间
void Call_me_exercise(std::string input);//叫我运动
bool Call_me_to_play(std::string place);//叫我出去玩
void Ask_for_my_personal_information();//询问我的个人信息
private:
//成员变量(属性)
char My_eyes[1000];//我看到的
char My_nose[1000];//我闻到的
char My_mouth[1000];//我吃到的
char My_ears[1000];//我听到的
int My_gender;//我的性别 1为男性 0为女性
int My_height;//我的身高
int My_weight;//我的体重
int My_age;//我的年龄
bool My_girlfriend;//我是否有女朋友 0为没有 1为有
//成员函数(方法)
void My_Behavior_Walk();//我的走路行为
void My_Behavior_Think();//我的思考行为
void My_Behavior_Eat(std::string foodname);//我的吃东西行为
void My_Behavior_Sleep();//我的睡觉行为
void My_Behavior_learn();//我的学习行为
void My_Behavior_Play_Basketball();//我打篮球行为
void My_Behavior_Swim();//我的游泳行为
void My_Behavior_Play_Badminton();//我的打羽毛球行为
void My_Behavior_Play_ThePiano();//我的谈琴行为
};
}
#endif //__VISUAL_EAGLE_H__
Visual_eagle.cpp
#include<G:/program/vs2015/test/Visual_eagle.h>
namespace people
{
//构造函数
Visual_eagle::Visual_eagle()
{
My_gender = 1;
My_height = 192;
My_weight = 85;
My_age = 21;
My_girlfriend = false;
std::cout << "Construct Visual_eagle..." << std::endl;
std::cout << "I am Visual_eagle, nice to meet you (*_*)..." << std::endl;
}
//析构函数
Visual_eagle::~Visual_eagle()
{
std::cout << "See you later (*_*)..." << std::endl;
My_Behavior_Play_ThePiano();
My_Behavior_Sleep();
std::cout << "Destruct Visual_eagle" << std::endl;
}
//询问我的个人信息
void Visual_eagle::Ask_for_my_personal_information()
{
std::cout << "My_gender:" << My_gender << " '1' is male ,'0' is female" << std::endl;
std::cout << "My_height:" << My_height << "cm" << std::endl;
std::cout << "My_weight:" << My_weight << "kg" << std::endl;
std::cout << "My_age:" << My_age << std::endl;
std::cout << "My_girlfriend:" << My_girlfriend << " '0' is have,'1' is have not" << std::endl;
}
//叫我吃饭,输入值是你想吃啥,距离有多远,预计吃一顿饭花多久时间
void Visual_eagle::Call_me_to_eat(std::string food_name, int dist, int time)
{
//听
int c;
for (c = 0; c < food_name.length(); c++)
{
My_ears[c] = food_name[c];
}
My_ears[c] = '\0';
std::cout << "I heard to eat " << My_ears << std::endl;
My_Behavior_Think();
if (dist >= 2 || time > 3)
{
std::cout << "Too far or too long, sorry i can't go ..." << std::endl;
}
else
{
My_Behavior_Walk();
//闻
int b;
for (b = 0; b < food_name.length(); b++)
{
My_nose[b] = food_name[b];
}
My_nose[b] = '\0';
std::cout << "I smell the smell of" << My_nose << std::endl;
//看
int a;
for (a = 0; a < food_name.length(); a++)
{
My_eyes[a] = food_name[a];
}
My_eyes[a] = '\0';
std::cout << "I saw " << My_eyes << std::endl;
//吃
int i;
for (i = 0; i < food_name.length(); i++)
{
My_mouth[i] = food_name[i];
}
My_mouth[i] = '\0';
std::cout << "I ate " << My_mouth << std::endl;
My_Behavior_Eat(food_name);
}
}
//叫我运动,输入值是具体运动
void Visual_eagle::Call_me_exercise(std::string input)
{
My_Behavior_Think();
if (input == "Play_Basketball")
{
My_Behavior_Play_Basketball();
}
else if (input == "Swim")
{
My_Behavior_Swim();
}
else if (input == "Play_Badminton")
{
My_Behavior_Play_Badminton();
}
else
{
std::cout << "I won't, but i can learn !" << std::endl;
My_Behavior_learn();
}
}
//叫我出去玩,返回值是我去还是不去1 or 0
bool Visual_eagle::Call_me_to_play(std::string place)
{
std::string data = place;
My_Behavior_Think();
return false;
}
//我的走路行为
void Visual_eagle::My_Behavior_Walk()
{
std::cout << "I am walking and listening to music...(@_@)" << std::endl;
_sleep(2 * 1000);
std::cout << "End behavior... " << std::endl;
}
//我的思考行为
void Visual_eagle::My_Behavior_Think()
{
std::cout << "I am thinking..." << std::endl;
_sleep(4 * 1000);
std::cout << "End behavior... " << std::endl;
std::cout << "The problem is solved!!!(+_+)" << std::endl;
}
//我的吃东西行为
void Visual_eagle::My_Behavior_Eat(std::string foodname)
{
char* food = (char*)foodname.c_str();
std::cout << "I am eating..." << std::endl;
_sleep(2 * 1000);
std::cout << "End behavior... " << std::endl;
std::cout << "The taste of " << food << " is delicious!!!(+_+)" << std::endl;
std::cout << "I am stuffed...(+_+)" << std::endl;
}
//我的睡觉行为
void Visual_eagle::My_Behavior_Sleep()
{
std::cout << "I am sleeping..." << std::endl;
_sleep(6 * 1000);
std::cout << "End behavior... " << std::endl;
std::cout << "I woke up and my energy regained!!!(+_+)" << std::endl;
}
//我的学习行为
void Visual_eagle::My_Behavior_learn()
{
std::cout << "I am learning...(?_?)" << std::endl;
_sleep(2 * 1000);
std::cout << "End behavior... " << std::endl;
std::cout << "I have learnt it !!!(+_+)" << std::endl;
}
//我打篮球行为
void Visual_eagle::My_Behavior_Play_Basketball()
{
std::cout << "I'm playing basketball ...(?_?)" << std::endl;
std::cout << "Shot..." << std::endl;
_sleep(2 * 1000);
srand((int)time(0));
int goals_number = 0;
for (int i = 0; i < 100; i++)
{
std::cout << rand() % 100 << " ";
if (rand() % 100 > 50)
{
goals_number++;
}
}
std::cout << "Number of goals:" << goals_number << " ,hit rate:" << goals_number << "%" << std::endl;
std::cout << "End behavior... " << std::endl;
}
//我的游泳行为
void Visual_eagle::My_Behavior_Swim()
{
std::cout << "I'm swimming ...(@_@)" << std::endl;
std::cout << "Float up..." << std::endl;
_sleep(2 * 1000);
std::cout << "Dive..." << std::endl;
std::cout << "Float up..." << std::endl;
std::cout << "End behavior... " << std::endl;
}
//我的打羽毛球行为
void Visual_eagle::My_Behavior_Play_Badminton()
{
std::cout << "I'm playing badminton ...(?_?)" << std::endl;
_sleep(2 * 1000);
std::cout << "End behavior... " << std::endl;
}
//我的谈琴行为
void Visual_eagle::My_Behavior_Play_ThePiano()
{
std::cout << "I'm playing the piano ...(?_?)" << std::endl;
_sleep(2 * 1000);
std::cout << "End behavior... " << std::endl;
}
}
main.cpp
#include<iostream>
#include<G:/program/vs2015/test/Visual_eagle.h>
int main(int argv, char *argc[])
{
/*1.创建一个我自己*/
//栈中创建我这个类
people::Visual_eagle visual_eagle;
//堆中创建
//Visual_eagle* v = new Visual_eagle();
//问我个人信息
visual_eagle.Ask_for_my_personal_information();
//叫我吃饭,输入值是你想吃啥,距离有多远,预计吃一顿饭花多久时间
visual_eagle.Call_me_to_eat("rice1", 4, 1);
visual_eagle.Call_me_to_eat("rice2", 1, 4);
visual_eagle.Call_me_to_eat("Steamed bun",1,1);
//叫我运动,输入值是具体运动
visual_eagle.Call_me_exercise("Play_TableTennis");
visual_eagle.Call_me_exercise("Play_Basketball");
visual_eagle.Call_me_exercise("Swim");
visual_eagle.Call_me_exercise("Play_Badminton");
//叫我出去玩,返回值是我去还是不去1 or 0
if (visual_eagle.Call_me_to_play("park"))
{
std::cout << "go go go" << std::endl;
}
else
{
std::cout << "sorry,I can't go..." << std::endl;
}
//关于继承
std::cout << "father: public protected private" << std::endl;
std::cout << "son:" << std::endl;
std::cout << "public public protected no" << std::endl;
std::cout << "protected protected protected no" << std::endl;
std::cout << "private private private no" << std::endl;
std::cout << "see you tomorrow" << std::endl;
return 0;
}
继承方面的问题我就不以自己这个类来举例子了。。。明天继续更新
标签:std,eagle,系列,cout,void,c++,Behavior,写成,My 来源: https://blog.csdn.net/visual_eagle/article/details/122282692