C++基础(九)C++的构造函数:构造函数的多态

一、简介

主要参考:http://c.biancheng.net/view/149.html

二、构造函数的多态

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

class Complex {
private:
	double real, imag;
public:
	Complex(double r);
	Complex(double r, double i);
	Complex(Complex cl, Complex c2);
};
Complex::Complex(double r)  //构造函数 1
{
	real = r;
	imag = 0;
}
Complex::Complex(double r, double i)  //构造数 2
{
	real = r;
	imag = i;
}
Complex::Complex(Complex cl, Complex c2)  //构造函数 3
{
	real = cl.real + c2.real;
	imag = cl.imag + c2.imag;
}
int main() {
	Complex cl(3), c2(1, 2), c3(cl, c2), c4 = 7;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xpj8888/article/details/85272299