OOP HomeWork
This commit is contained in:
57
oop_hw5/hw2/animal.cpp
Normal file
57
oop_hw5/hw2/animal.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "animal.h"
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
Animal::Animal() {
|
||||
name = "#UNDEFINED";
|
||||
weight = -1;
|
||||
}
|
||||
Animal::Animal(std::string n, int w) {
|
||||
name = n;
|
||||
weight = w;
|
||||
}
|
||||
std::string Animal::show_name()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
int Animal::show_weight()
|
||||
{
|
||||
return weight;
|
||||
}
|
||||
void Animal::who() {
|
||||
std::cout << "This is an Animal" << std::endl;
|
||||
std::cout << "Name : " << name << std::endl
|
||||
<< "Weight : " << weight << std::endl;
|
||||
}
|
||||
Lion::Lion() {
|
||||
this->change_name("#UNDEFINED_LION_NAME");
|
||||
this->change_weight(-1);
|
||||
}
|
||||
Lion::Lion(std::string n,int w) {
|
||||
this->change_name(n);
|
||||
this->change_weight(w);
|
||||
}
|
||||
void Lion::who() {
|
||||
std::cout << "This is a Lion" << std::endl;
|
||||
std::cout << "Name : " << this->show_name() << std::endl
|
||||
<< "Weight : " << this->show_weight() << std::endl;
|
||||
}
|
||||
Aardvark::Aardvark() {
|
||||
this->change_name("#UNDEFINED_LION_NAME");
|
||||
this->change_weight(-1);
|
||||
}
|
||||
Aardvark::Aardvark(std::string n, int w) {
|
||||
this->change_name(n);
|
||||
this->change_weight(w);
|
||||
}
|
||||
void Aardvark::who() {
|
||||
std::cout << "This is a Aardvark" << std::endl;
|
||||
std::cout << "Name : " << this->show_name() << std::endl
|
||||
<< "Weight : " << this->show_weight() << std::endl;
|
||||
}
|
||||
void Animal::change_name(std::string cn) {
|
||||
name = cn;
|
||||
}
|
||||
void Animal::change_weight(int cw) {
|
||||
weight = cw;
|
||||
}
|
34
oop_hw5/hw2/animal.h
Normal file
34
oop_hw5/hw2/animal.h
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
class Animal {
|
||||
private:
|
||||
std::string name;
|
||||
int weight;
|
||||
public:
|
||||
Animal();
|
||||
Animal(std::string n, int w);
|
||||
std::string show_name();
|
||||
int show_weight();
|
||||
void change_name(std::string cn);
|
||||
void change_weight(int cw);
|
||||
virtual void who();
|
||||
protected:
|
||||
|
||||
};
|
||||
|
||||
class Lion : public Animal {
|
||||
public:
|
||||
Lion();
|
||||
Lion(std::string n, int w);
|
||||
void who();
|
||||
};
|
||||
|
||||
class Aardvark : public Animal {
|
||||
public:
|
||||
Aardvark();
|
||||
Aardvark(std::string n, int w);
|
||||
void who();
|
||||
};
|
17
oop_hw5/hw2/main.cpp
Normal file
17
oop_hw5/hw2/main.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include "animal.h"
|
||||
|
||||
using std::cout;
|
||||
using std::cin;
|
||||
using std::endl;
|
||||
|
||||
int main() {
|
||||
Animal a("Animal!", 100);
|
||||
Lion b("Lion W", 200);
|
||||
Aardvark c("Aardvark", 300);
|
||||
a.who();
|
||||
b.who();
|
||||
c.who();
|
||||
return 0;
|
||||
}
|
BIN
oop_hw5/hw2/测试截图2.png
Normal file
BIN
oop_hw5/hw2/测试截图2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 329 KiB |
Reference in New Issue
Block a user