1. 普通类型 => 类类型
- 调用对应的只有一个参数【参数的类型就是这个普通类型】的构造函数
- 需求:
Boy boy1 = 10000; // 薪资 构造函数Boy(int);
Boy boy2 = “张三” // 姓名 构造函数Boy(string);
Boy.h
#pragma once
#include <string>
#include <iostream>
using namespace std;
class Boy{
public:
Boy(const string name = "无名", int age = 0, int salary = 0);
//boy1 = 10000就会调用该构造函数;
Boy(int salary);
//boy1 = "张三" 就会调用该构造函数;
Boy(const char* name);
friend ostream& operator<<(ostream& os, const Boy& boy);
private:
string name;//姓名
int age; //年龄
int salary; //薪资
};
Boy.cpp
#include <iostream>
#include <sstream>
#include "Boy.h"
Boy::Boy(const string name, int age, int salary){
this->name = name;
this->age = age;
this->salary = salary;
}
Boy::Boy(int salary){
this->salary = salary;
this->age = 0;
this->name = "无名";
}
Boy::Boy(const char* name){
this->name = name;
this->salary = 0;
this->age = 0;
}
main.cpp
#include <iostream>
#include <Windows.h>
#include "Boy.h"
using namespace std;
ostream