实验二
来源:互联网 发布:g92内螺纹编程实例解释 编辑:程序博客网 时间:2024/06/05 20:43
family_relationship.pro
#-------------------------------------------------
#
# Project created by QtCreator 2015-04-22T19:27:00
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = family_relationship
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
father.cpp \
mother.cpp \
child.cpp
HEADERS += \
father.h \
mother.h \
child.h
child.h
#ifndef CHILD_H#define CHILD_H#include <string>using namespace std;class Child{public:
Child();string name;void answer();void callFather();};#endif // CHILD_H
father.h
#ifndef FATHER_H#define FATHER_H#include <string>#include "child.h"using namespace std;class Father{public:
Father();string name;Child child;void callChild();void answer();};#endif // FATHER_H
mother.h
#ifndef MOTHER_H#define MOTHER_Hclass Mother{public:
Mother();};#endif // MOTHER_H
child.cpp
#include "child.h"#include "iostream"Child::Child():name("xiao Hua"){}void Child::answer(){cout<<endl<<name<<" is here!";}void Child::callFather(){cout<<endl<<"I am calling my father!";cout<<endl<<"Father is not here!";}
father.cpp
#include "father.h"#include <iostream>Father::Father()
:name("Lao Hua"){}void Father::callChild(){cout<<endl<<"I am calling my child!";child.answer();}void Father::answer(){cout<<endl<<name<<" is here waiting for you!";}
mother.cpp
#include "mother.h"Mother::Mother()
{}
main.cpp
#include <QCoreApplication>#include "father.h"#include <iostream>using namespace std;int main(int argc, char *argv[]){QCoreApplication a(argc, argv);Father baba;baba.callChild();
cout<<endl;
return a.exec();}1、通过C++课本复习2.5节(自定义数据类型)、4.2,3,4节(类和对象基本语法、类的数据成员和成员函数、构造和析构函数、类的组合)、6.6节(字符串)、7.1,3,4节(类的继承相关核心)内容,通过在Qt环境新建non-Qt项目实现这些章节中的代码;如果没有带C++课本,可在网上搜索相关知识,通过在Qt环境新建non-Qt项目写出练习代码并运行通过,记录过程中出现的各种编译器报错和运行期错误。
2、上网搜索资料,研究#include语句中,引用头文件时使用<和"的区别;在网上搜索C++的namespace关键字的相关资料,归纳namespace关键字的用法,写示例代码运行。
注意:一定要运行通过,给出运行结果,而不能只是在网上找到了相关知识贴在下面。
#include<> 引用的是编译器的类库路径里面的头文件。
#include"" 引用的是你程序目录的相对路径中的头文件.
命名空间是用来阻止和重用代码的,因为不能排除变量名即标识符相同的情况,对于库来说,这个问题尤为严重。为了解决这个问题,引入了命名空间这个概念。这样当对象来自不同的地方但是名字相同的时候就不会含糊不清了,是一种将程序库名称封装起来的方法,它就像在各个程序中立起来一道道围墙。
由于namespace的概念,使用C++标准程序的任何标识符时,可以有三种选择:1.直接指定标识符。2使用using关键字。3.使用using namespace std;
3、上网搜索“前置声明”,写练习代码,总结C++前置声明的语法、使用时的注意事项(包括为什么)、用途。
类A中用到了类B,而类B的声明出现在类A的后面。如果没有类B的前置说明,下面的程序将不同通过编译,编译器将会给出类似“缺少类型说明符”这样的出错提示
#include <iostream>
using namespace std;
class B;// 这是前置声明(Forward declaration)
class A
{
private:
B* b;
public:
A(B* b):b(b)
{
}
…
};
class B
{
…
};
// Main.cpp
#include "ForwardDeclaration.h"
int main(int argc, char** argv)
{
B* b = new B();
A* a = new A(b);
delete a;
delete b;
return 0;
}
4、参考附件family_relationship项目,完成:
(1)为Child类增添代码,时Child也能呼叫Father。尝试若不使用前置声明,或者不使用指针数据成员,或者不初始化对象中的指针成员时,可能出现的各种编译器报错或运行期错误,根据课本第46页3.1.3节“程序调试”,找到运行期错误的发生位置和原因。在网上搜索复习构造函数相关知识,在构造函数中写实验代码,用实验结果回答一个问题:如果没有执行任何初始化,那么,对象中的成员变量会被默认初始化为怎样的值?
如果不使用前置声明则会出现如下错误提示:显示Father类是一个没有被定义的类型,而且Child类内部没有father这一成员变量
如果不使用指针数据成员则会显示如下错误提示:提示Father是一个不完整的类型,从而使得主函数调用Child类时找不到father这个成员变量
如果不初始化对象中的指针成员时会显示如下出错信息:当程序执行到Child类的callfather()函数的father-〉answer()语句时因为主函数里面没有初始化对象中的指针成员则找不到father对象,因此就会出现如下错误。
如果没有只执行任何初始化,那么对象中的成员变量会默认为空字符串
(2)写Mother类代码,探索是否只能够采用前置声明和指针数据成员相结合的编程技术,尝试用两种不同编程技术实现Mother类和Child类。
Mother.h:
#ifndef MOTHER_H
#define MOTHER_H
#include <string>
#include "child.h"
using namespace std;
class Mother
{
public:
Mother();
string name;
Child child;
void callchild();
void answer();
};
#endif // MOTHER_H
Child.h:
#ifndef CHILD_H
#define CHILD_H
#include <string>
using namespace std;
class Father;
class Mother;
class Child
{
public:
Child();
Father * father;
Mother * mother;
string name;
void answer();
void callFather();
void callmother();
};
#endif // CHILD_H
Main.h:
#include <QCoreApplication>
#include "father.h"
#include"mother.h"
#include <iostream>
#include "child.h"
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplicationa(argc, argv);
Father * baba = new Father();
Mother * mama = new Mother();
Child kid = baba->child;
Child kid1= mama->child;
kid.father = baba;
kid1.mother = mama;
baba->callChild();
kid.callFather();
mama->callchild();
kid1.callmother();
cout<<endl;
return a.exec();
}
(3)为Father、Mother、Child类编写合适的构造函数,使他们能够互相呼叫,记录可能出现的各种编译器报错或运行期错误。
部分代码:
#include "father.h"
#include <iostream>
#include "child.h"
#include "mother.h"
Father::Father()
:name("Lao Hua")
{
child=new Child(this,0);
mother=new Mother(this,child);
child->mother=mother;
}
void Father::callChild(){
cout<<endl<<"I am calling my child!";
child->answer();
}
void Father::answer(){
cout<<endl<<name<<" is here waiting foryou!";
}
void Father::callWife(){
cout<<endl<<"I am calling my wife!";
mother->answer();
}
(4)尝试写一个Person类,使得Father、Mother和Child均继承自该类,记录过程中出现的各种编译器报错和运行期错误。
//person.h
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
string name;
virtual void answer()=0;
};
#endif // PERSON_H
//child.h
#ifndef CHILD_H
#define CHILD_H
#include "person.h"
#include <string>
using namespace std;
class Father;
class Mother;
class Child : public person
{
public:
Child();
Father *father;
Mother *mother;
void answer();
void callFather();
void callMother();
};
#endif
//mother.h
#ifndef MOTHER_H
#define MOTHER_H
#include "person.h"
#include <string>
using namespace std;
class Child;
class Mother :public person
{
public:
Mother();
Mother(Child *child);
Child *child;
void callChild();
void answer();
};
#endif
//father.h
#ifndef FATHER_H
#define FATHER_H
#include "person.h"
#include <string>
using namespace std;
class Child;
class Father :public person
{
public:
Father();
Father(Child *child);
Child *child;
void callChild();
void answer();
};
#endif
//child.cpp
#include "child.h"
#include "mother.h"
#include "father.h"
#include <iostream>
using namespace std;
Child::Child()
{
name="XIao Hua";
father=new Father(this);
mother=new Mother(this);
father->child=this;
mother->child=this;
}
void Child::answer(){
cout<<endl<<name<<" is here!"<<endl;
}
void Child::callFather(){
cout<<endl<<"I am calling my father!";
father->answer();
}
void Child::callMother(){
cout<<endl<<"I am calling my mother!";
mother->answer();
}
//mother.cpp
#include "mother.h"
#include "child.h"
#include <iostream>
using namespace std;
Mother::Mother()
{
name="Xiao Hua\'s Mother";
child=new Child();
child->mother=this;
child->father=NULL;
}
Mother::Mother(Child *child)
{
name="Xiao Hua\'s Mother";
this->child=child;
}
void Mother::callChild(){
cout<<endl<<name<<" say:I am calling mychild!";
child->answer();
}
void Mother::answer(){
cout<<endl<<"I\'m "<<name;
}
//father.cpp
#include "father.h"
#include "child.h"
#include <iostream>
using namespace std;
Father::Father()
{
name="Xiao Hua\'s Father";
child=new Child();
child->father=this;
child->mother=NULL;
}
void Father::callChild(){
cout<<endl<<name<<" say:I am calling mychild!";
child->answer();
}
Father::Father(Child *child)
{
name="Xiao Hua\'s Father";
this->child=child;
}
void Father::answer(){
cout<<endl<<"I\'m "<<name;
}
//main.cpp
#include "father.h"
#include "mother.h"
#include "child.h"
#include <iostream>
using namespace std;
int main()
{
Child xiaohua;
xiaohua.answer();
xiaohua.callFather();
xiaohua.callMother();
cout<<endl<<endl;
Mother xiaoma;
xiaoma.answer();
xiaoma.callChild();
Father xiaoba;
xiaoba.answer();
xiaoba.callChild();
return 0;
}
- 实验二 单链表实验
- 【实验二】串口实验
- 实验一&实验二
- 实验二
- 实验二
- 实验二
- 实验二
- 实验二
- 实验二
- 实验二
- 实验二
- 实验二
- 实验二
- 实验二
- 实验二
- 实验二
- 实验二
- 实验二
- 做好自动化运维平台必备技能
- hdu5975_Aninteresting game_树状数组理解
- Win10下运行复活之秦殇前传
- hdu5972 Regular Number
- [Leetcode]_33 Search in Rotated Sorted Array
- 实验二
- redis-cluster集群
- 守护进程
- 欢迎使用CSDN-markdown编辑器
- MySQL基本操作
- 微信小程序开发前的准备工作
- 经典排序算法——堆排序(基本算法之一)
- hdu 5974 A Simple Math Problem
- Python的并发编程