黑马程序员2017C++STL教程(9到11)

九. 普通类的 .h 和 .cpp分离编写模式
首先是 person.h
#include <iostream>
#include "person.h"
using namespace std;

int main()
{
    Person p("AAA",20);
    p.Show();
    return 0;
}

然后是person.cpp

#include "person.h"

Person::Person(string name, int age)
{
    this->mName = name;
    this->mAge = age;
}

void Person::Show(){
    cout<<"Name:"<<this->mName<<"  Age:"<<this->mAge<<endl;
}

最后是main.cpp

#include <iostream>
#include "person.h"
using namespace std;

int main()
{
    Person p("AAA",20);
    p.Show();
    return 0;
}
运行结果是
Name:AAA  Age:20
十. 类模板类内实现
#include <iostream>
#include <string>
using namespace std;

//类内实现类模板
template<class T1, class T2>
class Person{
public:
    Person(T1 name, T2 age)
    {
        this->mName = name;
        this->mAge = age;
    }

    void Show()
    {
        cout<<"Name:"<<this->mName<<" Age:"<<this->mAge<<endl;
    }
public:
    T1 mName;
    T2 mAge;
};

void test01()
{
    Person<string, int> p("AAA",20);
    p.Show();
}

int main()
{
    test01();
    return 0;
}

十一. 上午课程回顾(略)


猜你喜欢

转载自blog.csdn.net/garrulousabyss/article/details/80233504
今日推荐