+ 1

Can we create array of objects in oop? How to write it's syntax?

23rd Sep 2018, 4:20 AM
S.K
15 Réponses
+ 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; }
23rd Sep 2018, 5:40 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
S.K go through below link for updated code: https://code.sololearn.com/cT9rTlb9WUSh/?ref=app
23rd Sep 2018, 10:11 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
I can't tell how happy I'm, you are so so great, be happy. 😍😍 thanks
23rd Sep 2018, 12:02 PM
S.K
+ 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
23rd Sep 2018, 5:58 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 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
23rd Sep 2018, 6:29 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
23rd Sep 2018, 6:57 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
thank you Ketan Lalcheta, could you help me how to write array in constructor ??
23rd Sep 2018, 5:52 AM
S.K
0
thank you for your support. I need to have a constructor, and user should input details for 3 laptops. can you help pleas?
23rd Sep 2018, 6:20 AM
S.K
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
23rd Sep 2018, 6:21 AM
S.K
0
Ketan Lalcheta please review the above code. thanks
23rd Sep 2018, 6:22 AM
S.K
0
ohh yes
23rd Sep 2018, 6:47 AM
S.K
0
Ketan Lalcheta please find it in my codes. I have saved it there.
23rd Sep 2018, 6:50 AM
S.K
0
it's still incomplete... additionally, you can share that code's link into comment section which will take anybody to your code directly..
23rd Sep 2018, 6:56 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
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; } }
23rd Sep 2018, 7:28 AM
S.K
0
Ketan Lalcheta please check it now. I have pasted complete code. I think I haven't created array in it. plz help
23rd Sep 2018, 7:31 AM
S.K