• .h文件和.cpp文件


    //新建如图文件

    //在头文件.h中声明,在.cpp中实现

    //main.cpp代码如下

     1 #define  _CRT_SECURE_NO_WARNINGS
     2 #include<iostream>
     3 #include "test.h"
     4 #include <string.h>
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     //Student st ;//栈上,这个类的构造函数在类被定义为变量的时候,自动调用
    10     Student *p = new Student("mike",10);//在堆上创建实例,不管在堆上,还是在栈里面,只要这个类成了一个对象,构造函数都会自动被调用(带参的函数)。
    11     p->show();
    12     //p->add();//父类中是private
    13     /*strcpy(p->name, "tom");
    14     p->age = 10;*/
    15     //p->set_money(500);
    16     cout << "name = " << p->name << ",age = " << p->age<<endl;
    17     cout << "money = " << p->get_money()<<endl;
    18     delete p;//自动调用析构函数
    19     system("pause");
    20     return 0;
    21 }
    View Code

    //person.cpp代码

     1 #include "person.h"
     2 #include <stdio.h>
     3 
     4 Person::Person()
     5 {
     6 
     7 }
     8 
     9 void Person::show()
    10 {
    11     printf("show
    ");
    12 }
    13 
    14 void Person::add()
    15 {
    16     printf("add
    ");
    17 }
    View Code

    //test.cpp

     1 #define  _CRT_SECURE_NO_WARNINGS
     2 #include "test.h"
     3 #include <stdio.h>
     4 #include <string.h>
     5 
     6 void Student::set_money(int n)
     7 {
     8     money = n;
     9 }
    10 int Student::get_money()
    11 {
    12     return money;
    13 }
    14 
    15 
    16 Student::Student(const char *_name, int _age, int _money)
    17 {
    18     strcpy(name, _name);//this
    19     age = _age;
    20     money = _money;
    21 }
    22 Student::Student()
    23 {
    24     memset(name, 0, sizeof(name));
    25     age = 0;
    26     money = 0;
    27     printf("hello world!
    ");
    28     //classes = new char[100];//在构造函数中分配了100个char。
    29 }
    30 Student::~Student()
    31 {
    32     printf("调用了析构函数。
    ");
    33     //delete[]classes;//清理构造函数分配的堆空间内存。
    34 }
    View Code

    //person.h

     1 #ifndef PERSON_H
     2 #define PERSON_H
     3 
     4 
     5 class Person
     6 {
     7 private:
     8     void add();
     9 public:
    10     Person();
    11     void show();
    12 };
    13 #endif // !PERSON_H
    View Code

    //test.h

     1 #ifndef TEST_H
     2 #define TEST_H
     3 
     4 #include "person.h"
     5 
     6 class Student : public Person
     7 {
     8 public:
     9     char name[10];
    10     char *classes;
    11     int age;
    12 private:
    13     int money;
    14 public:
    15     void set_money(int n);
    16     int get_money();
    17     Student(const char *_name, int _age,int money = 0);//构造函数重载
    18     Student();
    19     ~Student();
    20 };
    21 
    22 #endif    
    View Code

    //运行结果

  • 相关阅读:
    高进度乘法FFT优化
    Activity的四种加载模式
    异步任务AsyncTask
    利用Handler在子线程中更新UI
    Android 屏幕旋转监听
    HDOJ-1698-线段树成段更新
    HDOJ-1671-字典树
    HDOJ-1251 字典树
    python数据结构与算法
    find the lowest number location
  • 原文地址:https://www.cnblogs.com/linst/p/5189548.html
Copyright © 2020-2023  润新知