设计一个类,用对象数组,堆对象和对象拷贝建立该类的对象,统计建立的对象数
怎么来设计解决这个
3条回答 默认 最新
快乐鹦鹉 2022-10-31 16:43关注堆对象?就是new出来呗?对象数组是存储这个设计出来的类?
是要这样吗?#include <iostream> using namespace std; class A { protected: static int count; int n; public: A() {count++;} A(int n):n(n) {count++;} A(const A& a) {n = a.n;count++;} static int getCount() {return count;} }; int A::count = 0; int main() { int n,m=0; A *p = NULL; A as[10]; cin>>n; while(n>0) { if(n!=m) { A *a = new A(n); p = a; m = n; } else A *a = new A(*p); cin>>n; } cout<<"共创建"<<A::getCount()<<"个A对象"<<endl; return 0; }本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报 编辑记录解决 1无用