C++例子

#include<iostream>
using namespace std;

//抽象类
//抽象cpu类
class CPU
{
public:
   virtual void calcuate()=0;
};
//抽象显卡类
class VideoCard
{
public:
   virtual void display()=0;
};
//抽象内存条类
class Memory
{
public:
  virtual void storage()=0;

};

//电脑类
class Computer
{
public:
  //构造函数
  Computer(CPU *cpu, VideoCard *vc, Memory *mem)
  {
   m_cpu = cpu;
   m_vc = vc;
   m_mem = mem;
  }
  //什么行为,提供变量工作的函数
  void work()
  {
    m_cpu->calcuate();
    m_vc->display();
    m_mem->storage();
  }
  ~Computer()
{
  if(m_cpu!=NULL)
  {
    delete m_cpu;
    m_cpu =NULL;
  }
if(m_vc!=NULL)
  {
    delete m_vc;
    m_vc =NULL;
  }
if(m_mem!=NULL)
  {
    delete m_mem;
    m_mem =NULL;
  }

}

private:
// 这个类先写private
   CPU *m_cpu;     //定义一个CPU的指针
   VideoCard *m_vc;
   Memory *m_mem;


};

//具体厂商
//intel厂商
class IntelCPU : public CPU
{
public:
   virtual void calcuate()
   {
    cout<<"Intel的CPU开始计算了!"<<endl;
   }

};

class IntelVideoCard: public VideoCard
{
public:
   virtual void display()
   {
    cout<<"Intel的显卡开始计算了!"<<endl;
   }
};

class IntelMemory: public Memory
{
public:
   virtual void storage()
   {
    cout<<"Intel的存储开始计算了!"<<endl;
   }
};

// lenovo
class LenovoCPU : public CPU
{
public:
   virtual void calcuate()
   {
    cout<<"Intel的CPU开始计算了!"<<endl;
   }

};

class LenovoVideoCard: public VideoCard
{
public:
   virtual void display()
   {
    cout<<"Intel的显卡开始计算了!"<<endl;
   }
};

class LenovoMemory: public Memory
{
public:
   virtual void storage()
   {
    cout<<"Intel的存储开始计算了!"<<endl;
   }
};


void test01()
{
    //第一台电脑零件
    //new返回的是同类型的指针
    //int *p= new int  或int[10]
    CPU *IntelCpu = new IntelCPU;
    VideoCard * IntelCard = new IntelVideoCard;
    Memory *IntelMem = new IntelMemory;
//创建第一台电脑
    Computer * computer1= new Computer(IntelCpu,IntelCard,IntelMem);
    computer1->work();
    delete computer1;
    //method1  继续释放new的
    // 放在析构中
    // delete IntelCPU;
    // delete IntelCard;
    // delete IntelMem;
//创建第二台
    cout<<"创建第二胎"<<endl;
    CPU *LenovoCpu = new LenovoCPU;
    VideoCard * LenovoCard = new LenovoVideoCard;
    Memory *LenovoMem = new LenovoMemory;
    //或者直接new
    // Computer *computer2 = new Computer( new LenovoCPU,new LenovoVideoCard,new LenovoMemory);
    Computer * computer2 = new Computer(LenovoCpu,LenovoCard,LenovoMem);
    computer2->work();
    delete computer2;

}
int main()
{
  test01();
//   system("pause");
  return 0;
}

在这里插入图片描述
在这里插入图片描述
cpu还没给m_cpu时,
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
析构其它的new

二、

// #include<iostream>
// using namespace std;

// //抽象类
// //抽象cpu类
// class CPU
// {
// public:
//    virtual void calcuate()=0;
// };
// //抽象显卡类
// class VideoCard
// {
// public:
//    virtual void display()=0;
// };
// //抽象内存条类
// class Memory
// {
// public:
//   virtual void storage()=0;

// };

// //电脑类
// class Computer
// {
// public:
//   //构造函数
//   Computer(CPU *cpu, VideoCard *vc, Memory *mem)
//   {
//    m_cpu = cpu;
//    m_vc = vc;
//    m_mem = mem;
//   }
//   //什么行为,提供变量工作的函数
//   void work()
//   {
//     m_cpu->calcuate();
//     m_vc->display();
//     m_mem->storage();
//   }
//   ~Computer()
// {
//   if(m_cpu!=NULL)
//   {
//     delete m_cpu;
//     m_cpu =NULL;
//   }
// if(m_vc!=NULL)
//   {
//     delete m_vc;
//     m_vc =NULL;
//   }
// if(m_mem!=NULL)
//   {
//     delete m_mem;
//     m_mem =NULL;
//   }

// }

// private:
// // 这个类先写private
//    CPU *m_cpu;     //定义一个CPU的指针
//    VideoCard *m_vc;
//    Memory *m_mem;


// };

// //具体厂商
// //intel厂商
// class IntelCPU : public CPU
// {
// public:
//    virtual void calcuate()
//    {
//     cout<<"Intel的CPU开始计算了!"<<endl;
//    }

// };

// class IntelVideoCard: public VideoCard
// {
// public:
//    virtual void display()
//    {
//     cout<<"Intel的显卡开始计算了!"<<endl;
//    }
// };

// class IntelMemory: public Memory
// {
// public:
//    virtual void storage()
//    {
//     cout<<"Intel的存储开始计算了!"<<endl;
//    }
// };

// // lenovo
// class LenovoCPU : public CPU
// {
// public:
//    virtual void calcuate()
//    {
//     cout<<"Intel的CPU开始计算了!"<<endl;
//    }

// };

// class LenovoVideoCard: public VideoCard
// {
// public:
//    virtual void display()
//    {
//     cout<<"Intel的显卡开始计算了!"<<endl;
//    }
// };

// class LenovoMemory: public Memory
// {
// public:
//    virtual void storage()
//    {
//     cout<<"Intel的存储开始计算了!"<<endl;
//    }
// };


// void test01()
// {
//     //第一台电脑零件
//     //new返回的是同类型的指针
//     //int *p= new int  或int[10]
//     CPU *IntelCpu = new IntelCPU;
//     VideoCard * IntelCard = new IntelVideoCard;
//     Memory *IntelMem = new IntelMemory;
// //创建第一台电脑
//     Computer * computer1= new Computer(IntelCpu,IntelCard,IntelMem);
//     computer1->work();
//     delete computer1;
//     //method1  继续释放new的
//     // 放在析构中
//     // delete IntelCPU;
//     // delete IntelCard;
//     // delete IntelMem;
// //创建第二台
//     cout<<"创建第二胎"<<endl;
//     CPU *LenovoCpu = new LenovoCPU;
//     VideoCard * LenovoCard = new LenovoVideoCard;
//     Memory *LenovoMem = new LenovoMemory;
//     //或者直接new
//     // Computer *computer2 = new Computer( new LenovoCPU,new LenovoVideoCard,new LenovoMemory);
//     Computer * computer2 = new Computer(LenovoCpu,LenovoCard,LenovoMem);
//     computer2->work();
//     delete computer2;

// }
// int main()
// {
//   test01();
// //   system("pause");
//   return 0;
// }


#include<iostream>
using namespace std;

class base
{

public:
// 无参构造
   base()
   {
	cout<<"父类构造"<<endl;
   }

   ~base()
   {
    cout<<"父类析构"<<endl;

   }

};
class son :public base
{
public:
	son()
	{
		cout<<"儿子构造"<<endl;
	}

  ~son()
   {
    cout<<"儿子析构"<<endl;

   }


};


int main()
{
   son x;
   return 0;
}

输出结果:

父类构造
儿子构造
儿子析构
父类析构

debug信息

Breakpoint 1, main () at /mnt/workspace/test/test.cpp:208
208     {
(gdb) s
209        son x;
(gdb) 
son::son (this=0x7ffea3799fe7) at /mnt/workspace/test/test.cpp:193
193             {
(gdb) 
base::base (this=0x7ffea3799fe7) at /mnt/workspace/test/test.cpp:179
179             cout<<"父类构造"<<endl;
(gdb) 
父类构造
180        }
(gdb) 
son::son (this=0x7ffea3799fe7) at /mnt/workspace/test/test.cpp:194
194                     cout<<"儿子构造"<<endl;
(gdb) 
儿子构造
195             }
(gdb) 
main () at /mnt/workspace/test/test.cpp:210
210        return 0;
(gdb) 
211     }
(gdb) 
son::~son (this=0x7ffea3799fe7, __in_chrg=<optimized out>) at /mnt/workspace/test/test.cpp:199
199         cout<<"儿子析构"<<endl;
(gdb) 
儿子析构
201        }
(gdb) 
base::~base (this=0x7ffea3799fe7, __in_chrg=<optimized out>) at /mnt/workspace/test/test.cpp:184
184         cout<<"父类析构"<<endl;
(gdb) 
父类析构
186        }
(gdb) 

#include
using namespace std;

class base
{
public:
virtual void sample()=0;
};

class son :public base
{
public:
virtual void sample()
{
cout<<“快得很”<<endl;
}

};

int main()
{
   son x;
   //调用方法才有输出
   x.sample();


   son *p = new son;
   p->sample();
   delete p;
   return 0;
}

输出两次,
快得很
快得很

3)

#include<iostream>
using namespace std;

class base
{
public:
 base(int &m,int &n)
 {
  m1=m;
  n1=n;
 
 }
 void prit()
 {

   cout<<"m:"<<m1<<"n:"<<n1<<endl;

 }
  // virtual void sample()=0;
  void  fun(){}
private:
  int m1;
  int n1;
};


class son :public base
{
public:
  virtual void sample()
  {
   cout<<"快得很"<<endl;
  }


};


int main()
{
  //  son x;
  //  //调用方法才有输出
  //  x.sample();
  int c=10;
  int b =20;
  //抽象类不能直接直接实例化
  base a(c,b);
  a.prit();


抽象类不能直接直接实例化,去掉虚幻书,加上fun的一个空定义

相关推荐

  1. <span style='color:red;'>C</span>++<span style='color:red;'>例子</span>

    C++例子

    2024-03-24 19:02:01      19 阅读
  2. C均值算法例子

    2024-03-24 19:02:01       27 阅读
  3. c# 类的方法链接例子

    2024-03-24 19:02:01       24 阅读
  4. C#中的委托概念以及例子

    2024-03-24 19:02:01       33 阅读
  5. C语言指针简介及例子

    2024-03-24 19:02:01       7 阅读
  6. C/C++内存越界的常有例子

    2024-03-24 19:02:01       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-24 19:02:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-24 19:02:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-24 19:02:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-24 19:02:01       20 阅读

热门阅读

  1. pytest 之数据驱动

    2024-03-24 19:02:01       22 阅读
  2. 八个 C++ 开源项目,帮助初学者进阶成长

    2024-03-24 19:02:01       18 阅读
  3. 基于单片机的农业智能节水灌溉系统设计

    2024-03-24 19:02:01       17 阅读
  4. 数学建模(Topsis python代码 案例)

    2024-03-24 19:02:01       20 阅读
  5. Pytorch:torch.cuda.empty_cache()

    2024-03-24 19:02:01       16 阅读
  6. trt | torch2trt的使用方式

    2024-03-24 19:02:01       21 阅读
  7. P7480 Reboot from Blue 线段树优化建图跑最短路

    2024-03-24 19:02:01       20 阅读
  8. 和neo4j相似的python工具

    2024-03-24 19:02:01       19 阅读
  9. 【2024-03-20】华为春招笔试三道编程题解

    2024-03-24 19:02:01       17 阅读