Commit 23c992e2 authored by 刘睿博's avatar 刘睿博 🎯

ver 1.0

parents
build/
.cache/
\ No newline at end of file
cmake_minimum_required(VERSION 3.5.0)
project(stl_debug VERSION 0.1.0 LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 17)
INCLUDE_DIRECTORIES(
include
)
add_subdirectory(src)
#pragma once
#include <string>
class Human
{
private:
/* data */
std::string name_;
int age_;
public:
explicit Human(int age, const std::string name = "") : name_(name), age_(age) {};
virtual ~Human();
virtual void print() const;
};
#pragma once
#include "Human.hpp"
class Student : public Human
{
private:
/* data */
std::string school_;
public:
explicit Student(int age, const std::string name = "", const std::string school = "") : Human(age, name), school_(school) {};
virtual ~Student() override;
virtual void print() const override;
};
\ No newline at end of file
add_executable(
stl_test main.cpp Human.cpp Student.cpp
)
\ No newline at end of file
#include "Human.hpp"
Human::~Human() {
printf("Human destructor called\n");
}
void Human::print() const {
printf("My name is %s and I am %d years old\n", name_.c_str(), age_);
}
\ No newline at end of file
#include "Student.hpp"
Student::~Student() {
printf("Student destructor called\n");
}
void Student::print() const {
Human::print();
printf("I'm from %s\n", school_.c_str());
}
\ No newline at end of file
#include <stdio.h>
#include "Human.hpp"
#include "Student.hpp"
#include <vector>
int main(int, char**){
printf("Hello, from stl_debug!\n");
std::vector<Human*> vec;
Human human(25, "John Doe");
Student student(20, "Jane Doe", "MIT");
vec.push_back(&human);
vec.push_back(&student);
for (const auto& h : vec) {
h->print();
}
static_cast<Student*>(vec.back())->print();
}
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