
关于C++中类的继承还有vector向量的问题,请问下面程序哪些地方出错了,应该怎么改
#include<iostream>#include<vector>classBase{public:Base(inti){foo=i;}virtual~Base(){}...
#include<iostream>
#include<vector>
class Base
{
public:
Base(int i)
{
foo = i;
}
virtual ~Base()
{}
void print()
{
std::cout<< foo << std::endl;
}
protected:
int foo;
private:
Base(const Base&rhs) : foo(rhs.foo){}
};
class A : public Base
{
public:
A(int i) :Base(i){}
};
int main( int argc, const char *argv[])
{
std::vector<A> foo;
foo.push_back(A(100));
foo.push_back(200);
for(size_t i = 0; i<foo.size(); ++i)
{
foo[i].print();
}
return 0;
} 展开
#include<vector>
class Base
{
public:
Base(int i)
{
foo = i;
}
virtual ~Base()
{}
void print()
{
std::cout<< foo << std::endl;
}
protected:
int foo;
private:
Base(const Base&rhs) : foo(rhs.foo){}
};
class A : public Base
{
public:
A(int i) :Base(i){}
};
int main( int argc, const char *argv[])
{
std::vector<A> foo;
foo.push_back(A(100));
foo.push_back(200);
for(size_t i = 0; i<foo.size(); ++i)
{
foo[i].print();
}
return 0;
} 展开
1个回答
展开全部
#include<iostream>
#include<vector>
class Base
{
public:
Base(){}
Base(int i)
{
foo = i;
}
virtual ~Base()
{}
void print()
{
std::cout<< foo << std::endl;
}
public:
int foo;
private:
Base(const Base&rhs) : foo(rhs.foo){}
};
class A : public Base
{
public:
A(){}//无参构造函数
A(int i) :Base(i){}
A(const A&a){this->foo = a.foo;} //要赋值拷贝函数
};
int main( int argc, const char *argv[])
{
std::vector<A> foo;
foo.push_back(A(100));
foo.push_back(A(200));
for(size_t i = 0; i<foo.size(); ++i)
{
foo[i].print();
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询