+ 1
Can we create array of objects in oop? How to write it's syntax?
15 Respuestas
+ 2
S.K it's very similar to other data types :
#include <iostream>
using namespace std;
class student
{
string name;
public :
void setname(string str){name = str;}
};
int main()
{
student s[3];
s[0].setname("Ketan");
return 0;
}
+ 2
S.K go through below link for updated code:
https://code.sololearn.com/cT9rTlb9WUSh/?ref=app
+ 2
I can't tell how happy I'm, you are so so great, be happy. 😍😍 thanks
+ 1
S.K refer below :
#include <iostream>
using namespace std;
class student
{
string name;
public :
student(string str){name = str;}
};
int main()
{
student s[3] = {student("Ketan"),student("Ketan1"),student("Ketan2")};
return 0;
}
let me know if you want something else
+ 1
seems it is incomplete code and you are unable to paste entire due to size limit... please copy link of code and paste it here to have a look into it
+ 1
like this :
https://code.sololearn.com/cgovh9lKEe1Q/?ref=app
0
thank you Ketan Lalcheta, could you help me how to write array in constructor ??
0
thank you for your support.
I need to have a constructor, and user should input details for 3 laptops.
can you help pleas?
0
Please help me figure out this problem. I enter data for 3 laptops and loop prints data of the last laptop only for 3 times.
#include <iostream>
#include <string>
using namespace std;
class laptop
{
private:
string brand;
string model;
int serial;
string color;
float price;
float processor_Speed;
int ram;
float screen_Size;
public:
void display()
{
cout<<"Brand: "<<brand<<endl;
cout<<"Model: "<<model<<endl;
cout<<"Serial No. "<<serial<<endl;
cout<<"Color: "<<color<<endl;
cout<<"Price: "<<price<<endl;
cout<<"Processor Speed: "<<processor_Speed<<endl;
cout<<"RAM :"<<ram<<endl;
cout<<"Screen Size: "<<screen_Size<<endl;
cout<<"-------------------"<<endl;
}
laptop(string b, string m, int s, string c, float p, float ps,int r, float sz)
{
brand = b;
model = m;
serial = s;
color = c;
price = p;
processor
0
Ketan Lalcheta please review the above code. thanks
0
ohh yes
0
Ketan Lalcheta please find it in my codes. I have saved it there.
0
it's still incomplete... additionally, you can share that code's link into comment section which will take anybody to your code directly..
0
#include <iostream>
#include <string>
using namespace std;
class laptop
{
private:
string brand;
string model;
int serial;
string color;
float price;
float processor_Speed;
int ram;
float screen_Size;
public:
void display()
{
cout<<"Brand: "<<brand<<endl;
cout<<"Model: "<<model<<endl;
cout<<"Serial No. "<<serial<<endl;
cout<<"Color: "<<color<<endl;
cout<<"Price: "<<price<<endl;
cout<<"Processor Speed: "<<processor_Speed<<endl;
cout<<"RAM :"<<ram<<endl;
cout<<"Screen Size: "<<screen_Size<<endl;
cout<<"-------------------"<<endl;
}
laptop(string b, string m, int s, string c, float p, float ps,int r, float sz)
{
brand = b;
model = m;
serial = s;
color = c;
price = p;
processor_Speed = ps;
ram = r;
screen_Size = sz;
}
};
int main()
{
string brand;
string model;
int serial;
string color;
float price;
float processor_Speed;
int ram;
float screen_Size;
for (int i = 0; i < 3; i++)
{
cout << "Laptop model :" << i + 1 << endl << endl;
cout << "Brand name (string):";
cin >> brand;
cout << "Modal name (string):";
cin >> model;
cout << "Serial name (int):";
cin >> serial;
cout << "color name (string):";
cin >> color;
cout << "Price (int):";
cin >> price;
cout << "processor speed (float):";
cin >> processor_Speed;
cout << "RAM (int):";
cin >> ram;
cout << "Screen size (float):";
cin >> screen_Size;
cout << endl << endl;
}
cout << "____________________________________" << endl;
for (int i = 0; i < 3; i++)
{
cout << "Laptop Model :" << i + 1 << endl << endl;
laptop details(brand, model, serial, color, price, processor_Speed, ram, screen_Size);
details.display();
cout << endl << endl << endl;
cout << "__________________________________" << endl;
}
}
0
Ketan Lalcheta please check it now. I have pasted complete code.
I think I haven't created array in it. plz help