Commit 0db19a25 authored by 刘睿博's avatar 刘睿博 🎯

1

parent 093a32f0
......@@ -9,6 +9,6 @@ private:
public:
explicit Human(int age, const std::string name = "") : name_(name), age_(age) {};
virtual ~Human();
virtual char* print() const;
virtual std::string print() const;
};
#pragma once
#include "Human.hpp"
#include <string>
class Student : public Human
{
......@@ -11,5 +12,5 @@ public:
Student() = delete;
explicit Student(int age, const std::string name = "", const std::string school = "") : Human(age, name), school_(school) {};
virtual ~Student() override;
virtual char* print() const override;
virtual std::string print() const override;
};
\ No newline at end of file
#include "Human.hpp"
#include <string>
#include <sstream>
Human::~Human() {
printf("Human destructor called\n");
}
char* Human::print() const {
char* res = new char[100];
sprintf(res, "My name is %s and I am %d years old\n", name_.c_str(), age_);
std::string Human::print() const {
std::string res;
std::stringstream ss;
ss << "My name is " << name_ << " and I am " << age_ << " years old\n";
ss >> res;
return res;
}
\ No newline at end of file
#include "Student.hpp"
#include "Human.hpp"
#include "cstring"
#include <sstream>
#include <string>
Student::~Student() {
printf("Student destructor called\n");
}
char* Student::print() const {
char *res = Human::print();
sprintf(res + strlen(res), "I'm from %s\n", school_.c_str());
std::string Student::print() const {
std::string res;
std::stringstream ss;
ss << Human::print();
ss << "I'm from " << school_ << "\n";
ss >> res;
return res;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment