C++ 多态、继承、多线程 (demo学习)

前言

作为菜鸟,为了成为C++牛人,特练习C++的牛逼特性,写了一个demo,包括C++继承、多态和简单的多线程,请各位多多指教。

直接上代码

main.cpp
/*
 * i'm new, just for testing C plus plus.
 * include inherit, multi state, multi thread
 *
 * author: Leo Fan
 * time  : 2019 12 23 18:40
 */
#include <iostream>
#include <memory>
#include <thread>
#include <pthread.h>
#include <mutex>
#include "warrior.h"

using namespace std;
void fire1(LingHuChong *ling, YueBuQun *yue);
void fire2(LingHuChong *ling, YueBuQun *yue);
static std::mutex m_lock;
int main()
{
  shared_ptr<DuGuJiuJian> dgjj(new DuGuJiuJian("D G J J", 100));
  shared_ptr<PiXieJianFa> pxjf(new PiXieJianFa("P X J F", 90));
  LingHuChong linghu(dgjj.get(), "LingHu", 24, 99);
  linghu.lprint();
  YueBuQun buqun(pxjf.get(), "BuQun", 50, 50);
  buqun.yprint();

  std::thread first(fire1, &linghu, &buqun);
  //fire1(&linghu, &buqun);

  ///
  linghu.setSelfName("genius");
  linghu.setSelfAge(26);
  buqun.setSelfName("false_man");
  buqun.setSelfAge(52);
  std::thread second(fire2, &linghu, &buqun);
  //fire2(&linghu, &buqun);
  first.join();
  second.join();
  return 0;
}

void fire1(LingHuChong *ling, YueBuQun *yue)
{
  m_lock.lock();
  int ling_fire_value = ling->getTalent()*ling->getDamageValue()/ling->getSelfAge();
  int yue_fire_value = yue->getTalent()*yue->getDamageValue()/yue->getSelfAge();
  m_lock.unlock();
  std::cout<<"first fire !"<<std::endl;
  if(ling_fire_value > yue_fire_value){
    std::cout<<"LingHuChong is win !"<<std::endl;
  }else{
    std::cout<<"YueBuQun is win !"<<std::endl;
  }
}
void fire2(LingHuChong *ling, YueBuQun *yue)
{
  m_lock.lock();
  int ling_fire_value = ling->getTalent()*ling->getDamageValue()/ling->getSelfAge();
  int yue_fire_value = yue->getTalent()*yue->getDamageValue()/yue->getSelfAge();
  m_lock.unlock();
  std::cout<<"second fire !"<<std::endl;
  if(ling_fire_value > yue_fire_value){
    std::cout<<"LingHuChong is win !"<<std::endl;
  }else{
    std::cout<<"YueBuQun is win !"<<std::endl;
  }
}
warrior class

.h文件

#ifndef WARRIOR_H_
#define WARRIOR_H_
#include <iostream>
#include "kongfu.h"

class DaXia{
public:
  DaXia(std::string name, int age, int talent_);
  virtual ~DaXia(){
    std::cout<<"DaXia is deleting !"<<std::endl;
  };
  virtual void setSelfName(std::string str_name);
  virtual void setSelfAge(int age);
  std::string getSelfName() const{return self_name_;}
  int getSelfAge() const{return self_age_;}
  int getTalent() const{return talent_;}
private:
  int talent_;
  std::string self_name_;
  int self_age_;
};

class LingHuChong : public DaXia
{
public:
  LingHuChong(JianFa *jf, std::string name, int age, int talent);
  ~LingHuChong() override{
    jf_ = nullptr;
    std::cout<<"ling is deleting !"<<std::endl;
  }
  void setSelfName(std::string str_name) override;
  void setSelfAge(int age) override;
  void lprint() const;
  int getDamageValue() const;
  int getTalent() const;
  std::string getSelfName() const{return DaXia::getSelfName();}
private:

  JianFa *jf_ = nullptr;
};

class YueBuQun : public DaXia
{
public:
  YueBuQun(JianFa *jf, std::string name, int age, int talent);
  ~YueBuQun() override{
    jf_ = nullptr;
    std::cout<<"yue is deleting !"<<std::endl;
  }
  void setSelfName(std::string str_name) override;
  void setSelfAge(int age) override;
  void yprint() const;
  int getDamageValue() const;
  int getTalent() const;
  std::string getSelfName() const{return DaXia::getSelfName();}
private:

  JianFa *jf_ = nullptr;
};
#endif

.cpp 文件

#include "warrior.h"

DaXia::DaXia(std::string name, int age, int talent)
  :self_name_(name),self_age_(age),talent_(talent)
{
  std::cout<<"DaXia is construction !!"<<std::endl;
}

inline void DaXia::setSelfName(std::string str_name)
{
  self_name_ = str_name;
}

void DaXia::setSelfAge(int age)
{
  self_age_ = age;
}

LingHuChong::LingHuChong(JianFa *jf, std::string name, int age, int talent)
  :DaXia( name, age, talent), jf_(jf)
{
  std::cout<<" ling hu chong construction !"<<std::endl;
}

inline void LingHuChong::setSelfName(std::string str_name)
{
  DaXia::setSelfName(str_name);
  auto ling_name = [this](void)->void{
    std::cout<< "set ling hu chong name: " << this->DaXia::getSelfName() <<std::endl;
  };
  ling_name();
}

void LingHuChong::setSelfAge(int age)
{
  DaXia::setSelfAge(age);
  auto ling_age = [this](void)->void{
    std::cout<< "set ling hu chong age: " << DaXia::getSelfAge() <<std::endl;
  };
  ling_age();
}

void LingHuChong::lprint() const
{
  std::cout<<"name: "<<this->DaXia::getSelfName()<<" age: "<<this->DaXia::getSelfAge()<<
             " talent: "<<this->DaXia::getTalent();
  jf_->Jprint();
}

int LingHuChong::getDamageValue() const
{
  return jf_->getJanFaDamageValue();
}

int LingHuChong::getTalent() const
{
  return DaXia::getTalent();
}

YueBuQun::YueBuQun(JianFa *jf, std::string name, int age, int talent)
  :DaXia(name, age, talent), jf_(jf)
{
  std::cout<<" yue bu qun construction !"<<std::endl;
}

void YueBuQun::setSelfName(std::string str_name)
{
  DaXia::setSelfName(str_name);
  auto yue_name = [this](void)->void{
    std::cout<< "set yue bu qun name: " << this->DaXia::getSelfName() <<std::endl;
  };
  yue_name();
}

void YueBuQun::setSelfAge(int age)
{
  DaXia::setSelfAge(age);
  auto yue_age = [this](void)->void{
    std::cout<< "set yue bu qun age: " << DaXia::getSelfAge() <<std::endl;
  };
  yue_age();
}

void YueBuQun::yprint() const
{
  std::cout<<"name: "<<this->DaXia::getSelfName()<<" age: "<<this->DaXia::getSelfAge()<<
             " talent: "<<this->DaXia::getTalent();

  jf_->Jprint();
}

int YueBuQun::getDamageValue() const
{
  return jf_->getJanFaDamageValue();
}

int YueBuQun::getTalent() const
{
  return DaXia::getTalent();
}
kongfu class

.h文件

#ifndef KONGFU_H_
#define KONGFU_H_
#include <iostream>

class KongFu{
 public:
  virtual ~KongFu(){
    //std::cout<<"pur deleting must be defined !"<<std::endl;
  }
  virtual std::string getKongFuName() const=0;
  virtual int getDamageValue() const=0;
  virtual void Jprint() const=0;
};

class JianFa : public KongFu{
 public:
  explicit JianFa(std::string janfa_name, int damage_value);
  virtual ~JianFa() override{}
  std::string getKongFuName() const override;
  int getDamageValue() const override;
  void setJanFaName(std::string janfa_name);
  void setJanFaDamageValue(int damage_value);
  std::string getJanFaName() const{return janfa_name_;}
  int getJanFaDamageValue() const{return damage_value_;}
  virtual void Jprint() const override;
 private:
  std::string janfa_name_;
  int damage_value_;
};

class DuGuJiuJian : public JianFa{
 public:
  DuGuJiuJian(std::string jf_name, int damage_value);
  ~DuGuJiuJian() override{}
  std::string getJanFaName() const;
  int getJanFaDamageValue() const;
  void Jprint() const override;
};

class PiXieJianFa : public JianFa{
 public:
  PiXieJianFa(std::string jf_name, int damage_value);
  ~PiXieJianFa() override{}
  std::string getJanFaName() const;
  int getJanFaDamageValue() const;
  void Jprint() const override;
};
#endif

.cpp文件

#include "kongfu.h"

JianFa::JianFa(std::string janfa_name, int damage_value)
  :janfa_name_(janfa_name), damage_value_(damage_value)
{
  std::cout<<"jan fa is construction !"<<std::endl;
}

std::string JianFa::getKongFuName() const
{
  return janfa_name_;
}

int JianFa::getDamageValue() const
{
  return damage_value_;
}

void JianFa::setJanFaName(std::string janfa_name)
{
  janfa_name_ = janfa_name;
}

void JianFa::Jprint() const
{
  std::cout<<"  Jian Fa name: "<<getJanFaName()<<" damage_value: "<<getJanFaDamageValue()<<std::endl;
}

//DuGuJiuJian::DuGuJiuJian(std::string jf_name, int damage_value)
//  :JianFa(jf_name, damage_value)
//{
//  std::cout<<"du gu jiu jian is construction !"<<std::endl;
//}

DuGuJiuJian::DuGuJiuJian(std::string jf_name, int damage_value)
  :JianFa(jf_name, damage_value){
  std::cout<<"du gu jiu jian is construction !"<<std::endl;
}

std::string DuGuJiuJian::getJanFaName() const
{
  return JianFa::getJanFaName();
}

int DuGuJiuJian::getJanFaDamageValue() const
{
  return JianFa::getJanFaDamageValue();
}

void DuGuJiuJian::Jprint() const
{
  std::cout<<"  Du Gu Jiu Jian ";
  JianFa::Jprint();
}

//PiXieJianFa::PiXieJianFa(std::string jf_name, int damage_value)
//  :JianFa(jf_name, damage_value)
//{
//  std::cout<<" pi xie jian fa is construction !"<<std::endl;
//}

PiXieJianFa::PiXieJianFa(std::string jf_name, int damage_value)
  :JianFa(jf_name, damage_value){
  std::cout<<" pi xie jian fa is construction !"<<std::endl;
}

std::string PiXieJianFa::getJanFaName() const
{
  return JianFa::getJanFaName();
}

int PiXieJianFa::getJanFaDamageValue() const
{
  return JianFa::getJanFaDamageValue();
}

void PiXieJianFa::Jprint() const
{
  std::cout<<"  Pi Xie Jan Fa ";
  JianFa::Jprint();
}

last

编译的时候要链接pthread (ubuntu),有问题直接留言。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值