有关c++
构建一个类book,其中含有两个私有数据成员qu和price,将qu初始化为1~5,将price初始化为qu的10倍。建立一个含5个元素的对象数组,显示每个对象数组元素的qu*price.
通过对象指针访问对象数组,使程序以相反的顺序显示每个对象数组元素的qu*price.
不会用指针了,救救孩纸吧! #include <iostream>
#include <algorithm>
#include <random>
#include <vector>
using namespace std;
class Book
{
public:
Book()
{
//产生1 - 5的随机数
random_device r;
default_random_engine e1(r());
uniform_int_distribution<int> uniform_dist(1, 5);
qu = uniform_dist(e1);
price = 10 * qu;
}
int QuTimesPrice() { return qu * price; }
private:
int qu;
int price;
};
int main()
{
vector<Book*> arr;
for (size_t i = 0; i < 5; ++i)
arr.emplace_back(new Book());
for (size_t i = 5; i > 0; --i)
cout << arr->QuTimesPrice() << " ";
cout << endl;
system("pause");
return 0;
} #include<iostream>
using namespace std;
class book
{
private:
int qu;
int price;
public:
book(int q,int p)
{
qu=q;
price=p;
}
intsi()
{
cout<<qu*price<<endl;
}
};
int main()
{
book ob={
book(1,10),
book(2,20),
book(3,30),
book(4,40),
book(5,50)
};
book *p;
for(p=&ob; p!=ob; p--)
{
p->si();
}
ob->si();
}
页:
[1]