继承
继承
Class public private
class Enemy
{
public:
int Damage;
void Attack();
Enemy(int hp,int damage);
Enemy();
int GetHP();
void Attack();
private:
void Attack();
int HP;
};
class Enemy
{
public:
int HP;
int Damage;
void Attack();
void TakeDamage();
Enemy(int hp,int damage);//构造函数
Enemy();
};
Enemy::Enemy(int hp,int damage){
HP=hp;
Damage=damage;
}
Enemy::Enemy(){
HP=1000;
Damage=1000;
}
int main()
{
Enemy enemy=Enemy(100,10);//我们使用自己定义的类声明的变量,可以成为对象
enemy.HP=100;
enemy.Damage=10;
cout<<enemy.HP<<endl;
cout<<enemy.Damage<<endl;
enemy.Attack();
enemy.TakeDamage();
cout<<enemy.HP<<endl;
Enemy enemy2=Enemy();//因为提供了带有参数的构造函数,所以系统不提供无参的构造函数
cout<<enemy2.HP<<endl;
cout<<enemy2.Damage<<endl;
enemy2.TakeDamage();
enemy2.TakeDamage();
cout<<enemy2.HP<<endl;
string str;
cin>>str;
return 0;
}
类的实现、对象的定义和成员的调用
头文件
class Enemy
{
public:
int HP;
int Damage;
void Attack();
void TakeDamage();
};
源文件
void Enemy::Attack(){
cout<<"播放攻击动作"<<endl;
cout<<"播放攻击特效"<<endl;
cout<<"让别攻击的受伤"<<endl;
}
void Enemy::TakeDamage(){
cout<<"播放受伤动作"<<endl;
cout<<"播放受伤特效"<<endl;
cout<<"减少自身血量"<<endl;
}
主文件
int main()
{
Enemy enemy=Enemy();//我们使用自己定义的类声明的变量,可以成为对象
cout<<enemy.HP<<endl;
cout<<enemy.Damage<<endl;
enemy.Attack();
enemy.TakeDamage();
Enemy enemy2=Enemy();
string str;
cin>>str;
return 0;
}
Class
类的声明
void skill1(){
cout<<"技能1"<<endl;
}
void skill2(){
cout<<"技能2"<<endl;
}
void skill3(){
cout<<"技能3"<<endl;
}
int main()
{
string name="东皇太一";
int hp;
int mp;
int attackPhysics;
int attackMagic;
int defensePhysics;
int defenseMagic;
string str;
cin>>str;
return 0;
}
头文件
#progma once
class Namexxxx {
//类的成员
public:
//
//
//
private:
//
//
//
};
class Enemy
{
public:
int HP;
int Damage;
void Attack();
void TakeDamage();
};
Class
void skill1(){
cout<<"技能1"<<endl;
}
void skill2(){
cout<<"技能2"<<endl;
}
void skill3(){
cout<<"技能3"<<endl;
}
int main()
{
string name="东皇太一";
int hp;
int mp;
int attackPhysics;
int attackMagic;
int defensePhysics;
int defenseMagic;
string str;
cin>>str;
return 0;
}
Pointer
指针类型和引用类型
#include<iotream>
#include<string>
using namespace std;
void change(int& a,int& b){
a=1000;
b=2000;
}
公寓大楼 内存卡
房间 内存空间 内存地址(房间号)
int main()
{
int a=324;
int b=100;
string str="sdluxncjdnchsdvhjcbsnxndjbj";
cout<<str<<endl;
//指针可以访问变量的内存地址
//指针就是用来存储某个变量的内存地址的
//int* pA;//int * pA;int *pA;
//pA = &a;
//指针* 引用& reference
//指针取地址,引用某个变量
int a=100;
int b=200;
int* p1 = &a;
cout<<p1<<endl;
p1 = &b;
cout<<p1<<endl;
int& r1=a;
cout<<r1<<":"<<a<<endl;
r1 = b;//a=b
cout<<r1<<":"<<a<<endl;
b=400;
cout<<r1<<":"<<a<<endl;
//引用的内存地址没法修改
//引用不可以指向新的变量
int a=0,b=0;
change(a,b);
cout<<a<<b<<endl;
string s;
cin>>s;
return 0;
}
Pointer
注意事项
#include<iotream>
#include<string>
using namespace std;
公寓大楼 内存卡
房间 内存空间 内存地址(房间号)
int main()
{
int a=324;
int b=100;
string str="sdluxncjdnchsdvhjcbsnxndjbj";
cout<<str<<endl;
//指针可以访问变量的内存地址
//指针就是用来存储某个变量的内存地址的
//int* pA;//int * pA;int *pA;
//pA = &a;
int* pA=&
cout<<pA<<endl;
pA=&b;
cout<<pA<<endl;
cout<<*pA<<endl;//*取得后面的(指针)内存地址所指向的数据(所存储的数据)
*pA=300//b=300
cout<<b<<":"<<*pA<<":"<<pA<<endl;
void* p1= &str; p1= &a;
cout<<*((omt*)p1)<<endl;
int* p2=NULL;
p2= &a;
string s;
cin>>s;
return 0;
}
Pointer
使用
取地址和取地址指向的数据
#include<iotream>
#include<string>
using namespace std;
公寓大楼 内存卡
房间 内存空间 内存地址(房间号)
int main()
{
int a=324;
int b=100;
string str="sdluxncjdnchsdvhjcbsnxndjbj";
cout<<str<<endl;
//指针可以访问变量的内存地址
//指针就是用来存储某个变量的内存地址的
//int* pA;//int * pA;int *pA;
//pA = &a;
int* pA=&
cout<<pA<<endl;
pA=&b;
cout<<pA<<endl;
cout<<*pA<<endl;//*取得后面的(指针)内存地址所指向的数据(所存储的数据)
*pA=300//b=300
cout<<b<<":"<<*pA<<":"<<pA<<endl;
string s;
cin>>s;
return 0;
}
//&取某个变量的地址;*取某个变量的数据
Pointer
声明和赋值
#include<iotream>
#include<string>
using namespace std;
公寓大楼 内存卡
房间 内存空间 内存地址(房间号)
int main()
{
int a=324;
string str="sdluxncjdnchsdvhjcbsnxndjbj";
cout<<str<<endl;
//指针可以访问变量的内存地址
//指针就是用来存储某个变量的内存地址的
int* pA;//int * pA;int *pA;
pA = &a;
int* pA=&
cout<<pA<<endl;
string s;
cin>>s;
return 0;
}
Pointer
#include<iotream>
#include<string>
using namespace std;
公寓大楼 内存卡
房间 内存空间 内存地址(房间号)
int main()
{
int s=324;
string s="sdluxncjdnchsdvhjcbsnxndjbj";
cout<<s<<endl;
//指针可以访问
string s;
cin>>s;
return 0;
}
数组
char cArray[10]={'a','b','3',' ','j'};
string strArray[10]={"siki","sikiedu","sikiedu.com","www.sikiedu.com"};
cout<<"char:"<<cArray[9]<<cArray[0]<<endl;
//NULL " "
if(strArray[9]==" "){
cout<<"Yes"<<endl;
}
int a='a';
cout<<"a:"<<a<<endl;
string str;
cin>>str;
数组初始化的多种方式
#include<iostream>
#include<string>
using namespace std;
int main(){
int enemHp=100;
//int enemyHPArray[10]={100,90,80,12,12,12,12,12,12,20};
//int enemyHPArray[]={100,90,80,12,12,12,12,12,12,20};
//int enemyHPArray[10]={100,90,80,12};
int enemyHPArray[10]={100,90,80,12,12,12,12,12,12,12,12,20};
//cout<<enemyHPArray[0]<<endl;
//cout<<enemyHPArray[9]<<endl;
enemyHPArray[3]=120;
int totalHp=0;
for(int i=0;i<10;i++){
totalHp+=enemyHPArray[i];
}
cout<<totalHp<<endl;
string str;
cin>>str;
return 0;
}
数组的声明、初始化和使用
#include<iostream>
using namespace std;
int main(){
int enemyHp=100;
//int enemy1Hp;
//int enemy2Hp;
int enemyHPArray[10];
enemyHPArray[0]=100;
enemyHPArray[1]=90;
enemyHPArray[2]=90;
enemyHPArray[3]=90;
enemyHPArray[4]=90;
enemyHPArray[5]=90;
enemyHPArray[6]=90;
enemyHPArray[7]=90;
enemyHPArray[8]=90;
enemyHPArray[9]=90;
cout<<enemyHPArray[0]<<endl;
return 0;
}
枚举类型
enum 枚举类型的名字{标识符列表};
//enum WeekDay{
//Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
//};
enum HeroType{
Master,Soldier,Assassin,Tnak
};
int main()
{
/*int weekday=1;
WeekDay day=Friday;
cout<<day<<endl;
int heroType=1;//1 法师 2 战士 3 刺客 4 坦克*/
HeroType heroType=Assassin;
heroType=Tank;
if(heroType==Master){
cout<<"This is Master."<<endl;
}
return 0;
}
猜数字游戏
#include<iostream>
#include<string>
#include<time.h>
using namespace std;
int main()
{
cout<<"Guess number"<<endl;
srand((int)time(0));
int number=rand()%10+1;
bool isSuccess=false;
for(int i=0;i<5;i++){
cout<<"I am thinking a number between 1-10,could you guess this number?You guess what:";
int numberGuess;
cin>>numberGuess;
if(numberGuess>number){
cout<<"It is larger than the number."<<endl;
}
else if(numberGuess==numbr){
isSucess=true;
cout<<"Congratulations,you are right!"<<endl;
break;
}
else{
cout<<"It is smaller than the number."<<endl;
}
}
if(isSucess){
}
else{
cout<<"You failed";
}
return 0;
}
猜数字游戏
#include<iostream>
#include<string>
#include<time.h>
using namespace std;
int main()
{
cout<<"Guess number"<<endl;
srand((int)time(0));
int number=rand()%10+1;
cout<<number<<endl;
return 0;
}
改进计算器小程序
bool RunCalculator();
int main()
{
cout<<"计算器!"<<endl;
while(true){
bool is Continue= RunCalculator();
if(isContinue==false)
break;
}
return 0;
}
bool RunCalculator(){
int operator1;
cout<<"Enter the first number:";
cin>>operator1;
int operator2;
cout<<"Enter the second number:";
cin>>operator2;
int res=operator1+operator2;
cout<<"Result is:"<<res<<endl;
cout<<"Continue calculating?(y/n)<<endl;
char c;
cin>>c;
if(c=='y'){
return true;
}
else{
return false;
}
}
auto关键字
auto x=9;//根据赋值,确定x的类型
int y=9;
函数的提前声明
名字 返回值 参数类型
int add(int,int);
结构体
把不同类型组合形成新的类型
struct 名字{
成员类型 成员名;
成员类型 成员名;
}
#include<iostream>
#include<string>
using namespace std;
struct Position{
float x;
float y;
float z;
};
struct Enemy
{
string name;
int hp;
int attack;
Position pos;
};
int main(){
/*float enemy1X;
float enemy1Y;
float enemy1Z;*/
Position enemy1Pos={100,20,90};
//enemy1Pos.x=90;
//enemy1Pos.y=10;
//enemy1Pos.z=12;
//cout<<enemy1Pos.y<<endl;
/*float enemy2X;
float enemy2Y;
float enemy2Z;*/
//Position enemy2Pos;
Enemy enemy1={"siki",100,50,{1,1,1}};
Enemy enemy2;
cout<<enemy1.pos.s<<endl;
return 0;
}