黑马C++ 小型案例汇总 2

文章提供了多个C++编程案例,涵盖了类的封装设计,如立方体类和点与圆的关系,展示了如何设置成员变量和方法以及如何比较对象。同时,文章也涉及到了多态的概念,通过计算器类和饮品制作类展示了抽象基类和虚函数的使用。此外,还利用STL进行了评委打分和员工分组的实例,演示了如何使用vector、deque、map和multimap进行数据处理和排序。
摘要由CSDN通过智能技术生成

104.封装设计案例1-立方体类

#include<iostream>
using namespace std;
class cube
{
    public:
    void setl(int a)
    {
        m_l=a;
    }
    void setw(int b)
    {
        m_w=b;
    }
    void seth(int c)
    {
        m_h=c;
    }
    int getl()
    {
        return m_l;
    }
    int getw()
    {
       return  m_w;
    }
    int geth()
    {
       return  m_h;
    }
    int area()
    {
        return 2*m_l*m_w+2*m_l*m_h+2*m_h*m_w;
    }
    int volume()
    {
        return m_l*m_w*m_h;
    }
    void issame(cube *c)
    {
        if (this->m_l==c->m_l&&this->m_w==c->m_w&&this->m_h==c->m_h)
        {
            cout<<"两立方体相等!"<<endl;
        }
        else{cout<<"不相等!"<<endl;}
    }
    int m_l;
    int m_w;
    int m_h;
};
void issame(cube *c1,cube *c2)
    {
        if (c1->m_l==c2->m_l&&c1->m_w==c2->m_w&&c1->m_h==c2->m_h)
        {
            cout<<"两立方体相等!"<<endl;
        }
        else{cout<<"不相等!"<<endl;}
    }
int main()
{
     cube c1;
     c1.setl(10);
     c1.setw(10);
     c1.seth(10);
     cout<<"面积为:"<<c1.area()<<endl;
     cout<<"体积为:"<<c1.volume()<<endl;
     cube c2;
     c2.setl(11);
     c2.setw(10);
     c2.seth(10);
     cout<<"面积为:"<<c2.area()<<endl;
     cout<<"体积为:"<<c2.volume()<<endl;
     c1.issame(&c2);
     issame(&c1,&c2);
}

105.封装设计案例-点和圆的关系

#include<iostream>
using namespace std;
class point 
{
    public:
    void setx(int a){p_x=a;}
    void sety(int b){p_y=b;}
    int  getx(){return this->p_x;}
    int  gety(){return this->p_y;}
    public:
    int p_x;
    int p_y;
};
class circle
{
    public:
    //circle(point p,int r):c_center(p),c_r(r){}
    void setr(int a)
    {
        this->c_r=a;
    }
    int getr()
    {
        return c_r;
    }
    void setcenter(point p)
    {
        c_center=p;
    }
    point getcenter()
    {
        
        return c_center;
    }
    void isincircle(point p)
    {
        if ((this->c_center.getx()-p.getx())*(this->c_center.getx()-p.getx())
           +(this->c_center.gety()-p.gety())*(this->c_center.gety()-p.gety())
           >this->getr())
        {
            cout<<"点在圆外!"<<endl;
        }
        else if ((this->c_center.getx()-p.getx())*(this->c_center.getx()-p.getx())
           +(this->c_center.gety()-p.gety())*(this->c_center.gety()-p.gety())
           <this->getr())
        {
            cout<<"点在圆内!"<<endl;
        }
        else{cout<<"点在圆上!"<<endl;}  
    }
    public:
    int c_r;
    point c_center;
};
int main()
{    circle c;
     point p;
     p.setx(0);
     p.sety(0);
     c.setr(10);
     c.setcenter(p);
     point p1;
     p1.setx(10);
     p1.sety(11);
     c.isincircle(p1);
     
    
    
    

    

    return 0;
}

137.多态案例-计算器类

#include<iostream>
using namespace std;
class caculate
{
   public:
   caculate(int a,int b):m_a(a),m_b(b){}
   int add(){return m_a+m_b;}
   int mul(){return m_a*m_b;}
   int m_a;
   int m_b;
};

int main()
{
    caculate c1(10,20);
    cout<<"加:"<<c1.add()<<endl;
    cout<<"乘:"<<c1.mul()<<endl;
    return 0;
}

139.多态案例-制作饮品

#include<iostream>
using namespace std;
class drinking
{
   public:
   virtual void boil()=0;
   virtual void brew()=0;
   virtual void pourcup()=0;
   virtual void putsomething()=0;
   void makedrink()
   {
    boil();
    brew();
    pourcup();
    putsomething();
   }
};
class coffe:public  drinking
{
   virtual void boil()
   {
    cout<<"煮水"<<endl;
   }
   virtual void brew()
   {
    cout<<"冲泡!"<<endl;
   }
   virtual void pourcup()
   {
    cout<<"倒杯!"<<endl;
   }
   virtual void putsomething()
   {
    cout<<"加糖!"<<endl;
   }
};
void dowork(drinking *dk)
{
     dk->makedrink();
     delete dk;
}

int main()
{
  dowork(new coffe);
  return 0;
}

142.多态案例3-电脑组装具体实现

#include<iostream>
using namespace std;
class cpu
{
   public:
   virtual void caculate()=0;
};
class xianka
{
   public:
   virtual void display()=0;
};
class memory
{
   public:
   virtual void cunchu()=0;
};
class computer
{
    public:
    computer(cpu *c,xianka *x,memory *m):m_cpu(c),m_xianka(x),m_memory(m){}
    void dowork()
    {
        m_cpu->caculate();
        m_xianka->display();
        m_memory->cunchu();
    }
    cpu *m_cpu;
    xianka *m_xianka;
    memory *m_memory;
};
class intercpu:public cpu
{
    public:
    virtual void caculate(){cout<<"inter的CPU工作了!"<<endl;}
};
class interxianka:public xianka
{
    public:
    virtual void display(){cout<<"inter的xianka工作了!"<<endl;}
};
class intermemory:public memory
{
    public:
    virtual void cunchu(){cout<<"inter的memory工作了!"<<endl;}
};

int main()
{
    cpu *inter1cpu=new intercpu;
    xianka *inter1xk=new interxianka;
    memory *inter1mem=new intermemory;
    computer *computer1=new computer(inter1cpu,inter1xk,inter1mem);
    computer1->dowork();
    return 0;
}

210.STL案例-评委打分

#include<iostream>
using namespace std;
//有5名选手:ABCDE,10个评委分别对每位选手打分,去掉最高分和最低分,取平均分
#include<vector>
#include<deque>
#include<algorithm>
class person{   //定义选手类  姓名和分数
    public:
        person(string name,int score){ 
    this->m_name=name;
    this->m_score=score;
}
   string m_name;
   int m_score;
};
void creatperson(vector<person> &v){  //创建选手
     string nameseed="ABCDE";       
     for (int i = 0; i < 5; i++)
     {
        string name="选手";      //姓名
        name+=nameseed[i];       
        int score=0;             //分数
        person p(name,score);    //姓名和分数传到人身上
        v.push_back(p);          //人再传到ector容器中
     }    
}
void setscore(vector<person> &v){
    for(vector<person>::iterator it=v.begin();it!=v.end();it++){
        deque<int> d;  //将评委的分数放到deque容器中
        for(int i=0;i<10;i++){
            int score=rand()%41+60;  //取随机数0到40+60=60到100
            d.push_back(score);      //将每一个分数都传给deque容器
        }
        cout<<it->m_name<<"的得分为:";
        for (deque<int>::iterator it=d.begin();it!=d.end();it++){
            cout<<*it<<" ";
        }

        sort(d.begin(),d.end());//将分数排序
        d.pop_back();  //去掉最低分和最高分
        d.pop_front();

        int sum=0;  //定义选手总分
        for(deque<int>::iterator dit=d.begin();dit!=d.end();dit++){
            sum+=*dit;
        }
        int avg=sum/d.size();  //定义平均分
        it->m_score=avg;      //将平均分赋值到选手身上
        cout<<"去掉一个最高分和一个最低分后,"<<it->m_name<<"的最后得分为:";
        cout<<it->m_score<<"分"<<endl;

    }  
}
void print(vector<person> &v){
    for(vector<person>::iterator it=v.begin();it!=v.end();it++){
        cout<<"姓名:"<<(*it).m_name<<"  分数为:"<<(*it).m_score<<endl;
    }
    cout<<endl;
}
int main(){
    srand((unsigned int)time(NULL));  //随即数种子
vector<person> v;//存放选手容器
creatperson(v);  // 创建选手
print(v);
setscore(v);
    system("pause");
    return 0;
}

222.list--排序案例

#include<iostream>
using namespace std;
#include<list>
class  person{
    public:
    person(string name,int age,int height){
        this->m_name=name;
        this->m_age=age;
        this->m_height=height;
    }
    string m_name;
    int m_age;
    int m_height;
};
void print(list<person> &l){
     for(list<person>::iterator it=l.begin();it!=l.end();it++){
        cout<<"姓名:"<<(*it).m_name<<" 年龄:"<<(*it).m_age<<" 身高:"<<(*it).m_height<<endl;
     }
}
bool mycompare(person &p1,person &p2){
    if(p1.m_age==p2.m_age){            //对于自定义数据类型
        return p1.m_height<p2.m_height;//需要指定排序规则
    }
    return p1.m_age<p2.m_age;
}
void test(){
    list<person> l;
    person p1("1111",111,11);
    person p2("222",22,2);
    person p3("3333",333,3);
    person p4("44",22,44);
    l.push_back(p1);
    l.push_back(p2);
    l.push_back(p3);
    l.push_back(p4);
    cout<<"排序前:"<<endl;
    print(l);
    cout<<"排序后:"<<endl;
    l.sort(mycompare);    //自定义数据类型,需要指定排序规则
    print(l);
}
int main(){
test();
    system("pause");
    return 0;
}

236.STL案例-员工分组

#include<iostream>
using namespace std;
#include<vector>
#include<map>
class person{         //1.创建员工:姓名和工资
    public:
    string m_name;
    int  m_salary;
};
void print(vector<person> &v){
    for(vector<person>::iterator it=v.begin();it!=v.end();it++){
        cout<<(*it).m_name<<(*it).m_salary<<endl;
    }
    cout<<endl;}
void setperson(vector<person> &v){//3.创建员工属性放入容器
    string nameseed="ABCDEFGHIJ";
    for (int i = 0; i < 10; i++)
    {
        person worker;
        worker.m_name="员工";        //定义姓名和工资
        worker.m_name+=nameseed[i];
        worker.m_salary=rand()%10000+10000; //1W-1.9W
        v.push_back(worker);        //员工传入容器
    }
}
void setg(vector<person> &v,multimap<int,person> &m){   //5.创建员工和部门所在容器
    for(vector<person>::iterator it=v.begin();it!=v.end();it++){//遍历每个员工
        int deptID=rand()%3; //定义部门 随机分为 第一第二第三部门
        m.insert(make_pair(deptID,*it));//将员工和部门插入map容器
    }
}
void show(multimap<int,person> &m){   //         6.显示各部门员工信息
    cout<<"策划部门员工为:"<<endl;
    multimap<int,person>::iterator pos=m.find(0); //找到该部门第一位员工 然后往后遍历
    int i=0;
    int count=m.count(0); //统计该部门人数
    for(;pos!=m.end()&&i<count;pos++,i++){  //打印员工,并往后遍历
        cout<<"姓名:"<<pos->second.m_name<<"  工资:"<<pos->second.m_salary<<endl;
    }
    cout<<"美术部门员工为:"<<endl;
    pos=m.find(1);
    i=0;
    count=m.count(1); //统计该部门人数
    for(;pos!=m.end()&&i<count;pos++,i++){  //遍历员工
        cout<<"姓名:"<<pos->second.m_name<<"  工资:"<<pos->second.m_salary<<endl;
    }
    cout<<"研发部门员工为:"<<endl;
    pos=m.find(2);
    i=0;
    count=m.count(2); //统计该部门人数
    for(;pos!=m.end()&&i<count;pos++,i++){  //遍历员工
        cout<<"姓名:"<<pos->second.m_name<<"  工资:"<<pos->second.m_salary<<endl;
    }
}
int main(){
    srand((unsigned int) time(NULL));
vector<person> v;  //2.创建容器装员工
setperson(v);
print(v);
multimap<int,person> m;  //4.将部门和员工放入一个容器
setg(v,m);     
show(m);          
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值