Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. simplifying code

simplifying code

Scheduled Pinned Locked Moved Managed C++/CLI
data-structures
5 Posts 4 Posters 25 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hshan_
    wrote on last edited by
    #1

    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

    V C 2 Replies Last reply
    0
    • H hshan_

      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

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      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?

      G 1 Reply Last reply
      0
      • H hshan_

        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

        C Offline
        C Offline
        Craig Robbins
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • V Victor Nijegorodov

          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?

          G Offline
          G Offline
          Ghazi Warrior
          wrote on last edited by
          #4

          if you will not add the break with case then they will execute either both after one to one

          H 1 Reply Last reply
          0
          • G Ghazi Warrior

            if you will not add the break with case then they will execute either both after one to one

            H Offline
            H Offline
            hshan_
            wrote on last edited by
            #5

            not relevant as i wanted to replace that method anyway my man ;P but yes i forgot about that

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups