simplifying code
-
i could use some tip about simplifying this code:
class Pojazd
{
public:virtual void buduj()=0;
};
class Sedan : Pojazd
{
private:
string typ="Sedan";
public:
string marka;
string model;
int hp;//horse power
double v;//pojemnosc silnika
int cena;
virtual void buduj()
{
cout << marka << " " << model << " " << endl;
cout << "Typ nadwozia: " << typ << endl;
cout << "Dane Techniczne: " << endl;
cout << "-" << hp << " KM" << "\n-Silnik: " << v << "L" << endl;
cout << "Cena " << cena << "\nmiesieczna rata " << cena / 12 << endl;
}
Sedan(string nadwozie)
{
nadwozie = typ;
}};
class Hatchback : Pojazd
{string typ;
public:
string marka;
string model;
int hp;//horse power
double v;//pojemnosc silnika
int cena;
virtual void buduj()
{
cout << marka << " " << model << " " << endl;
cout << "Typ nadwozia: " << typ << endl;
cout << "Dane Techniczne: " << endl;
cout << "-" << hp << " KM" << "\n-Silnik: " << v << "L" << endl;
cout << "Cena " << cena << "\nmiesieczna rata " << cena / 12 << endl;
}
Hatchback(string typ)
{
typ = typ;
}
};int main()
{
w:
int switch_on;
cout << "1.Sedan\n2.Hatchback"<> switch_on;
switch (switch_on)
{
case 1:
{
cout << "1.Mercedes\n2.BMW\n3.Audi\n";
cin >> switch_on;
switch (switch_on)
{
case 1:
{
Sedan Mercedes("Sedan");
Mercedes.marka = "Mercedes";
Mercedes.model = "A klasa";
Mercedes.hp = 145;
Mercedes.v = 2.1;
Mercedes.cena = 156000;Sedan\* wsk\_sedan; wsk\_sedan = &Mercedes; wsk\_sedan->buduj(); } case 2: { Sedan BMW("Sedan"); BMW.marka = "BMW"; BMW.model = "d531i"; BMW.hp = 166; BMW.v = 2.5; BMW.cena = 188770; Sedan\* wsk\_sedan; wsk\_sedan = &BMW; wsk\_sedan->buduj(); } }
}
break;
case 2:
{} break; } return 0;
}
i could continue with constructing this switch case tree sort of thingy but i'm wondering if i
-
i could use some tip about simplifying this code:
class Pojazd
{
public:virtual void buduj()=0;
};
class Sedan : Pojazd
{
private:
string typ="Sedan";
public:
string marka;
string model;
int hp;//horse power
double v;//pojemnosc silnika
int cena;
virtual void buduj()
{
cout << marka << " " << model << " " << endl;
cout << "Typ nadwozia: " << typ << endl;
cout << "Dane Techniczne: " << endl;
cout << "-" << hp << " KM" << "\n-Silnik: " << v << "L" << endl;
cout << "Cena " << cena << "\nmiesieczna rata " << cena / 12 << endl;
}
Sedan(string nadwozie)
{
nadwozie = typ;
}};
class Hatchback : Pojazd
{string typ;
public:
string marka;
string model;
int hp;//horse power
double v;//pojemnosc silnika
int cena;
virtual void buduj()
{
cout << marka << " " << model << " " << endl;
cout << "Typ nadwozia: " << typ << endl;
cout << "Dane Techniczne: " << endl;
cout << "-" << hp << " KM" << "\n-Silnik: " << v << "L" << endl;
cout << "Cena " << cena << "\nmiesieczna rata " << cena / 12 << endl;
}
Hatchback(string typ)
{
typ = typ;
}
};int main()
{
w:
int switch_on;
cout << "1.Sedan\n2.Hatchback"<> switch_on;
switch (switch_on)
{
case 1:
{
cout << "1.Mercedes\n2.BMW\n3.Audi\n";
cin >> switch_on;
switch (switch_on)
{
case 1:
{
Sedan Mercedes("Sedan");
Mercedes.marka = "Mercedes";
Mercedes.model = "A klasa";
Mercedes.hp = 145;
Mercedes.v = 2.1;
Mercedes.cena = 156000;Sedan\* wsk\_sedan; wsk\_sedan = &Mercedes; wsk\_sedan->buduj(); } case 2: { Sedan BMW("Sedan"); BMW.marka = "BMW"; BMW.model = "d531i"; BMW.hp = 166; BMW.v = 2.5; BMW.cena = 188770; Sedan\* wsk\_sedan; wsk\_sedan = &BMW; wsk\_sedan->buduj(); } }
}
break;
case 2:
{} break; } return 0;
}
i could continue with constructing this switch case tree sort of thingy but i'm wondering if i
hshan 2022 wrote:
switch (switch_on) { case 1: { Sedan Mercedes("Sedan"); Mercedes.marka = "Mercedes"; Mercedes.model = "A klasa"; Mercedes.hp = 145; Mercedes.v = 2.1; Mercedes.cena = 156000; Sedan* wsk_sedan; wsk_sedan = &Mercedes; wsk_sedan->buduj(); } case 2: { Sedan BMW("Sedan"); BMW.marka = "BMW"; BMW.model = "d531i"; BMW.hp = 166; BMW.v = 2.5; BMW.cena = 188770; Sedan* wsk_sedan; wsk_sedan = &BMW; wsk_sedan->buduj(); }
Don't you want to add the breaks at the end of these cases?
-
i could use some tip about simplifying this code:
class Pojazd
{
public:virtual void buduj()=0;
};
class Sedan : Pojazd
{
private:
string typ="Sedan";
public:
string marka;
string model;
int hp;//horse power
double v;//pojemnosc silnika
int cena;
virtual void buduj()
{
cout << marka << " " << model << " " << endl;
cout << "Typ nadwozia: " << typ << endl;
cout << "Dane Techniczne: " << endl;
cout << "-" << hp << " KM" << "\n-Silnik: " << v << "L" << endl;
cout << "Cena " << cena << "\nmiesieczna rata " << cena / 12 << endl;
}
Sedan(string nadwozie)
{
nadwozie = typ;
}};
class Hatchback : Pojazd
{string typ;
public:
string marka;
string model;
int hp;//horse power
double v;//pojemnosc silnika
int cena;
virtual void buduj()
{
cout << marka << " " << model << " " << endl;
cout << "Typ nadwozia: " << typ << endl;
cout << "Dane Techniczne: " << endl;
cout << "-" << hp << " KM" << "\n-Silnik: " << v << "L" << endl;
cout << "Cena " << cena << "\nmiesieczna rata " << cena / 12 << endl;
}
Hatchback(string typ)
{
typ = typ;
}
};int main()
{
w:
int switch_on;
cout << "1.Sedan\n2.Hatchback"<> switch_on;
switch (switch_on)
{
case 1:
{
cout << "1.Mercedes\n2.BMW\n3.Audi\n";
cin >> switch_on;
switch (switch_on)
{
case 1:
{
Sedan Mercedes("Sedan");
Mercedes.marka = "Mercedes";
Mercedes.model = "A klasa";
Mercedes.hp = 145;
Mercedes.v = 2.1;
Mercedes.cena = 156000;Sedan\* wsk\_sedan; wsk\_sedan = &Mercedes; wsk\_sedan->buduj(); } case 2: { Sedan BMW("Sedan"); BMW.marka = "BMW"; BMW.model = "d531i"; BMW.hp = 166; BMW.v = 2.5; BMW.cena = 188770; Sedan\* wsk\_sedan; wsk\_sedan = &BMW; wsk\_sedan->buduj(); } }
}
break;
case 2:
{} break; } return 0;
}
i could continue with constructing this switch case tree sort of thingy but i'm wondering if i
If you were going to give descriptions of many cars then the switch/case logic is not a good choice. Each time you add another car you need to change / compile / redistribute your application. Your descriptions of cars and their features should be outside the program. Look at the different kinds of ways that data can be stored, including but not limited to: tables, files, arrays and lists. You want to choose one that works long term. How many different types of cars will you include? How often will you make changes including corrections, additions and deletions? How do you want to make those updates? All these ideas need to be considered before you can choose the best solution.
-
hshan 2022 wrote:
switch (switch_on) { case 1: { Sedan Mercedes("Sedan"); Mercedes.marka = "Mercedes"; Mercedes.model = "A klasa"; Mercedes.hp = 145; Mercedes.v = 2.1; Mercedes.cena = 156000; Sedan* wsk_sedan; wsk_sedan = &Mercedes; wsk_sedan->buduj(); } case 2: { Sedan BMW("Sedan"); BMW.marka = "BMW"; BMW.model = "d531i"; BMW.hp = 166; BMW.v = 2.5; BMW.cena = 188770; Sedan* wsk_sedan; wsk_sedan = &BMW; wsk_sedan->buduj(); }
Don't you want to add the breaks at the end of these cases?
if you will not add the break with case then they will execute either both after one to one
-
if you will not add the break with case then they will execute either both after one to one