//新建如图文件

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

//main.cpp代码如下

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

//person.cpp代码

 #include "person.h"
#include <stdio.h> Person::Person()
{ } void Person::show()
{
printf("show\n");
} void Person::add()
{
printf("add\n");
}

//test.cpp

 #define  _CRT_SECURE_NO_WARNINGS
#include "test.h"
#include <stdio.h>
#include <string.h> void Student::set_money(int n)
{
money = n;
}
int Student::get_money()
{
return money;
} Student::Student(const char *_name, int _age, int _money)
{
strcpy(name, _name);//this
age = _age;
money = _money;
}
Student::Student()
{
memset(name, , sizeof(name));
age = ;
money = ;
printf("hello world!\n");
//classes = new char[100];//在构造函数中分配了100个char。
}
Student::~Student()
{
printf("调用了析构函数。\n");
//delete[]classes;//清理构造函数分配的堆空间内存。
}

//person.h

 #ifndef PERSON_H
#define PERSON_H class Person
{
private:
void add();
public:
Person();
void show();
};
#endif // !PERSON_H

//test.h

 #ifndef TEST_H
#define TEST_H #include "person.h" class Student : public Person
{
public:
char name[];
char *classes;
int age;
private:
int money;
public:
void set_money(int n);
int get_money();
Student(const char *_name, int _age,int money = );//构造函数重载
Student();
~Student();
}; #endif

//运行结果

.h文件和.cpp文件的更多相关文章

  1. vs 2015 项目筛选器没了,.h头文件和.cpp文件混在一起了

    场景: git 拉取 VS 2015 项目,打开之后,.h头文件和.cpp文件混在一起了. 解决方案: 需要XXX..vcxproj.filters 文件.

  2. C语言中头文件和cpp文件解析

    务必提前预读这里的内容:http://www.cnblogs.com/stemon/p/3999844.html 回到cpp文件与头文件各写什么内容的话题上: 理论上来说cpp文件与头文件里的内容,只 ...

  3. Qt中添加静态库.lb,.a和动态库.dll,.so,头文件和.cpp文件

    添加步骤 1.-Qt Creator中,"项目"------"添加库"2.把静态库和动态库文件放到项目文件夹中3.在.pro文件中会添加如下代码: - 添加动态 ...

  4. C中头文件在cpp文件中引用和.h文件引用

    1.编译器会单独编译每个cpp文件.头文件会复制到cpp文件中. 2.有时会遇到这样的一个问题a.cpp要调用b.cpp中的函数,而b.cpp又要调用a.cpp中的函数.这就牵扯到相互调用.这时如果我 ...

  5. C++中template的.h文件和.cpp文件的问题

    在C++中,用到类模板时,如果类似一般的类声明定义一样,把类声明放在.h文件中,而具体的函数定义放在.cpp文件中的话,会发现编译器会报错.如类似下面代码: //test.h文件 #ifndef TE ...

  6. c中头文件在cpp文件里引用和.h文件引用的思考

    我们在编敲代码中头文件是常常使用的. 可是头文件是应该包括在.H文件里还是在.cpp文件里.在这个其中有什么样去差别呢. 假如说我们编写了一个a.cpp  .我们将a.cpp文件的变量和函数申明在a. ...

  7. 头文件与cpp文件为什么要分开写

    最表面的机制是: 头文件是程序的界面(是代码界面),提供给程序员以  类.模版.函数等一系列的声明,让程序员知道应该怎么调用里面的"东西". 从动态链接库的角度看: 头文件提供界面 ...

  8. C++中头文件(.h)和源文件(.cpp)都应该写些什么

    头文件(.h): 写类的声明(包括类里面的成员和方法的声明).函数原型.#define常数等,但一般来说不写出具体的实现. 在写头文件时需要注意,在开头和结尾处必须按照如下样式加上预编译语句(如下): ...

  9. 在C的头文件中定义的结构体,如何在cpp文件中引用

    解决方案1:在cpp文件中放置.c,且在该文件中引用变量 解决方案2:在一个cpp文件中包含.c,但在另一个cpp文件中使用结构体变量 cpp文件1 cpp文件2 #include "dia ...

随机推荐

  1. ESSENTIAL ENGLISH SLANG

    airhead: stupid person. ace: excellent, great. Adam and Eve - Rhyming Slang for 'believe'. aggro - s ...

  2. Recommending music on Spotify with deep learning 采用深度学习算法为Spotify做基于内容的音乐推荐

    本文参考http://blog.csdn.net/zdy0_2004/article/details/43896015译文以及原文file:///F:/%E6%9C%BA%E5%99%A8%E5%AD ...

  3. angular2 学习笔记 ( ngModule 模块 )

    2016-08-25, 当前版本是 RC 5. 参考 : https://angular.cn/docs/ts/latest/guide/ngmodule.html 提醒 : 这系列笔记的 " ...

  4. MCS-51单片机I/O端口的存取

    MCS-51单片机通常有4个8位I/O端口, 向各端口的写数据均写入到对应端口的锁存器中, 但对各端口的读操作却有两个方式:读锁存器和读引脚 1 读-修改-写操作    Pn(指P0,P1,P2,P3 ...

  5. TCP 的那些事儿(转载)

    无论是PC客户端开发还是移动开发,或是Web开发, 网络编程都是很重要的一块, 深入理解TCP/IP和HTTP协议是一个优秀程序员的必备技能.看到酷壳老大正好写了篇相关文章, 正好拿来学习, 转自 h ...

  6. mono环境变量

    mono环境变量 2013-05-11 01:14:33|  分类: mono|举报|字号 订阅     下载LOFTER我的照片书  |     Name mono - Mono's ECMA-CL ...

  7. USACO6.4-Wisconsin Squares:搜索

    Wisconsin Squares It's spring in Wisconsin and time to move the yearling calves to the yearling past ...

  8. HDU3727--Jewel (主席树 静态区间第k大)

    Jewel Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. Objective-C priority queue

    http://stackoverflow.com/questions/17684170/objective-c-priority-queue PriorityQueue.h // // Priorit ...

  10. Ubuntu中apt-get出现E:Encountered a section with no Package: header……的解决方案

    方法一:运行命令apt-get update更新list列表 方法二:将/var/lib/apt/lists/下的所有list文件都删除,然后再update