主函数
#include "fun.h"
int main()
{
A x;
x.input();
x.show();
x.showavg();
A y(5);
y.input();
y.show();
y.showavg();
return 0;
}
源1
#include "fun.h"
A::A() {
b = 2;
}
A::A(int b1) {
b = b1;
}
void A::input() {
int i;
for (i = 0; i < b; i++) {
cout << "请输入x" << endl;
cin >> c[i][1];
cout << "请输入y" << endl;
cin >> c[i][2];
}
}
void A::show() {
int i;
for (i = 0; i < b; i++) {
cout << "(" << c[i][1] << "," << c[i][2] << ")" << endl;
}
}
void A::showavg() {
int i;
float av1=0, av2=0;
for (i = 0; i < b; i++) {
av1 += c[i][1];
av2 += c[i][2];
}
av1 = av1 / b;
av2 = av2 / b;
cout << "(" << av1 << "," << av2 << ")" << endl;
}
fun.h
#pragma once
#include <iostream>
using namespace std;
class A {
public:
A() ;
A(int b1) ;
~A() {};
void input() ;
void show() ;
void showavg();
private:
float c[100][2];
int b;
};
结论:析构函数的调用总是和构造函数次数相同,构造函数允许重载,其中形参的数量和类型可以通过区分不同重载函数 。构造函数调用时,若有继承,会先调用基类构造函数,结束时,会先调用子类析构函数。