zoukankan      html  css  js  c++  java
  • 基类和子类

    1. 对继承关系的描述转化为c++代码
    -class sublclass: public superClass{…} 
    
    -class pig : public Animal{…}

    1. 工具  --  这里介绍一款 编译器   code:Blocks svn bulid  各种集合
    #include <iostream>
    
    #include <string>
    
    class Animal   //this is a super
    
    {
    
    	public:
    
    	std::string mouth;
    
    	void eat ();// error: candidate is: void Animal::eat()
    
    	void sleep();
    
    	void drool();
    
    	
    
    };
    
    class pig:public Animal
    
    {
    
    	public :
    
    	void climb();
    
    	
    
    };
    
    class  turtle:public Animal
    
    {
    
    	public:
    
    	void swim();
    
    };
    
    void Animal::eat()
    
    {
    
    	std::cout<<"我在吃饭"<<std::endl;
    
    }
    
    void Animal::sleep()
    
    {
    
    	std::cout<<"我在睡觉,别打扰我"<<std::endl;
    
    }
    
    void Animal::drool()
    
    {
    
    	std::cout<<"我为什么在流口水,我要吃猪尾巴"<<std::endl;
    
    }
    
    void pig::climb()
    
    {
    
    	std::cout<<"你是母猪,你才上树呢"<<std::endl; 
    
    }
    
    void turtle::swim()
    
    {
    
    	std::cout<<"我是乌龟,我会游泳"<<std::endl;
    
    }
    
    int main(int argc, char *argv[])
    
    {
    
    	pig Pig;
    
    	turtle Turtle;
    
    	
    
    	Pig.eat();
    
    	Turtle.eat();
    
    	Pig.climb();
    
    	Turtle.swim();
    
    	
    
    	return 0;
    
    }

    结果:

    image

    image:需要注意  class 之后的分号 要记得写上

  • 相关阅读:
    WPF 便签项目
    .NET下WPF学习之Socket通信
    DEV控件
    字符串位数补足
    VS2008设置断点不命中
    错误描述: 242000021
    关闭Win10自带的 Windows Defender
    启用与关闭 Ad Hoc Distributed Queries
    Date工具类
    数据字段脱敏
  • 原文地址:https://www.cnblogs.com/yoyov5123/p/2938680.html
Copyright © 2011-2022 走看看