C++中的构造函数
来源:互联网 发布:java.util.zip 加密 编辑:程序博客网 时间:2024/06/16 08:06
Person p2();
Person p3("张二狗");
Person p4("张二狗", 1);
Person p5("张二狗", 1, 20);
p1.show();
只有4个构造函数,Person p2()并没有调用构造函数.
!!!!!!!!!!Person p2()其实是一个返回值为Person的函数,并不是初始化数据。这个书上已经说的很清楚了。。。。。
!!!!又犯错误了。。。。。
Person是个自定义的类
//Person头文件
#ifndef __COPYFUNCTION_H__
#define __COPYFUNCTION_H__
#include <string>
#include "hong.h"
class Person
{
private:
std::string name;
u_int number;
u_short age;
public:
~Person();
Person();
Person(std::string name);
Person(std::string name,u_int number);
Person(std::string name, u_int number, u_short age);
void show();
};
#endif
//Person cpp文件
#include "stdafx.h"
#include "copyfunction.h"
#include <iostream>
#include <string>
#include <iomanip>
#include "hong.h"
using std::cout;
using std::endl;
Person::~Person()
{
cout << "析构函数" << endl;
}
Person::Person() :name("0.0", 0, 0)
{
cout << "什么都不干的构造函数" << endl;
}
Person::Person(std::string s) :name(s)
{
cout << "只构造 name 的构造函数" << endl;
}
Person::Person(std::string s, u_int n) : name(s), number(n)
{
cout << "构造 name number 的构造函数" << endl;
}
Person::Person(std::string s, u_int n, u_short a) : name(s), number(n), age(a)
{
cout << "构造 name number age 的构造函数" << endl;
}
void Person::show()
{
cout.width(20);
cout << std::right << name << endl;
cout.width(20);
cout << std::right << number << endl;
cout.width(20);
cout << std::right << age << endl;
}
//宏文件
using u_int = unsigned int;
using u_short = unsigned short;
- C语言中的构造函数
- 2.c++与objective-c中的构造方法(构造函数)
- 关于Object-c 中的构造函数与析构函数
- objective-c中的构造函数(对象初始化)(七)
- objective-c中的构造函数(对象初始化)(七)
- C++-面试题:深度拷贝与构造函数中的异常
- 【C++】C++中的Sequence Point,构造函数等问题
- Objective-C中的便利初始化函数和便利构造器
- objective-c中的构造函数(对象初始化)(七)
- 初识c#---继承和继承关系中的构造函数
- C#中的构造函数
- 构造函数中的引用
- DotNet中的构造函数
- C#中的构造函数
- c#中的构造函数
- C#中的构造函数
- 枚举中的构造函数
- PHP中的构造函数
- MVP模式
- 20160519 学习日志
- 简单的图片查看器---ImageView的学习
- Google官方 由浅入深详解Fragment【附Fragment使用常见问题】
- Android studio 怎样导入 github
- C++中的构造函数
- mapReduce概念及用法
- MDWIKI
- dubbo服务者配置说明
- Android中的异步任务
- C# Sort排序
- STL-Intelligent IME
- POJ 3164 Command Network(最小树形图)
- 条件随机场CRF HMM,MEMM的比较