首页 > 其他分享 >每日打卡-1

每日打卡-1

时间:2023-04-10 21:34:34浏览次数:30  
标签:调用 weight 每日 age Mammal 打卡 public 构造函数

一.定义一个哺乳动物Mammal类,再由此派生出狗Dog类,定义一个Dog类的对象,观察基类与派生类的构造函数与析构函数的调用顺序。

1.我们按照题目要求写出对应代码,然后观察输出结果来确定调用顺序就好了。

2.

3.伪代码:

类 mammal{
public:
构造函数(){输出}
private:
age weight;
};
类 dog: public mammal{
public:
构造函数(){输出}
private:
name color
};
main(){
dog a;
return 0;
}

 

代码:

 

#include<iostream>
using namespace std;
class Mammal {

protected:
    int age, weight;
public:
    Mammal() :age(1), weight(5) { cout << "Mammal constructor..." << endl; }
    ~Mammal() { cout << "Mammal destructor..." << endl; }

    const int getage() {
        return age;
    }
    void setage(int a) {
        age = a;
    }
    const int getweight() {
        return weight;
    }
    void steweight(int a) {
        weight = a;
    }
};
class dog :public Mammal{

private:
    string color;
    string name;
public:
    dog():color("white"), name("xiaobai") { cout << "dog constructor..." << endl; }
    ~dog(){ cout << "dog destructor..." << endl; }
    string getcolor() {
        return color;
    }
    void setcolor(string s) {
        color = s;
    }
    string getname() {
        return name;
    }
    void setname(string s) {
        name = s;
    }
};
int main() {
    dog xiaobai;
    cout << "xiaobai is " << xiaobai.getage() << " years old" << endl;
    return 0;
}

4.总结:先调用基类的构造函数再调用派生类对象的构造函数

标签:调用,weight,每日,age,Mammal,打卡,public,构造函数
From: https://www.cnblogs.com/leapssisbird/p/17304376.html

相关文章

  • c++基础 打卡1
    一、面向对象的编程语言有的特点。    ①面向对象的编程语言最大的特点是结构化程序,二结构化程序的设计思路是自顶向下、逐步求精;其程序化结构是按功能划分为若干个基本模块,这些模块形成一个树状结构;各模块之间的关系尽可能简单,在功能上相对独立;每个模块内部均是由顺序、......
  • 打卡一小时
    #include<iostream>#include<cmath>#include"Rectangle.h"usingnamespacestd;intmain(){   Rectanglerect;   rect.intiRectangle(2,3,20,10);   rect.move(3,2);   cout<<"Thedateofrect(x,y,w,h):"<&l......
  • 2023.4.10每日总结
    html分页展示<%@pageimport="wangzhan.Pd_zhengce"%><%@pageimport="wangzhan.Thesql"%><%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!D......
  • 2023.4.9每日总结
    <%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"&g......
  • 每日总结 4..9
    今天对我的售卖机页面设计。   实现androd的数据传输,和补货的数据更新。packagecom.example.jjt;importandroid.util.Log;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava......
  • 每日总结2023-04-09
    今天完成了密码找回界面代码:<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools=&qu......
  • 每日总结2023-04-08
    今天实现了AndroidStudio的高德地图APK定位packagecom.example.math.www_user;importandroidx.annotation.NonNull;importandroidx.appcompat.app.AppCompatActivity;importandroid.Manifest;importandroid.os.Bundle;importandroid.util.Log;importandroid.wid......
  • 每日总结 4.8
    今天进行了蓝桥杯的考试,考完试后进行了androdi的补货功能。代码如下:packagecom.example.jjt;importandroidx.appcompat.app.AppCompatActivity;importandroid.app.VoiceInteractor;importandroid.os.Bundle;importandroid.os.Handler;importandroid.view.View;imp......
  • [补][leetcode每日一题]1.28
    1664. 生成平衡数组的方案数提示中等107相关企业给你一个整数数组 nums 。你需要选择 恰好 一个下标(下标从 0 开始)并删除对应的元素。请注意剩下元素的下标可能会因为删除操作而发生改变。比方说,如果 nums=[6,1,7,4,1] ,那么:选择删除下标 1 ,剩下的数组为 nums=[6,7,......
  • 每日总结2023-04-07
    今天对前几天的的界面做了优化packagecom.example.math;/**注册界面*/importandroidx.annotation.NonNull;importandroidx.appcompat.app.AppCompatActivity;importandroid.content.Intent;importandroid.os.Build;importandroid.os.Bundle;importandroid.os......