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. Design and Architecture
  4. how to design base class?

how to design base class?

Scheduled Pinned Locked Moved Design and Architecture
designtutorialquestion
7 Posts 5 Posters 0 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.
  • W Offline
    W Offline
    wan rui qq com
    wrote on last edited by
    #1

    today I discuss with my friends .we conculde that there is two ways design base class . 1 .we design the method whether belongs to all class or only belongs to sub-class into base-class the code writed like that

    class CVehicle
    {
    public:
    virtual void fly()
    {
    cout<<"I'm sorry I don't have method fly"<

    2 .we think that sub class has its own method so we should design like that

    class CVehicle
    {
    public:

    };

    class CAeroplane:public CVehicle
    {
    public:
    void fly()
    {
    cout<<"I am a Aeroplane i can fly "<

    so which way will the best way we should order ?

    W P 2 Replies Last reply
    0
    • W wan rui qq com

      today I discuss with my friends .we conculde that there is two ways design base class . 1 .we design the method whether belongs to all class or only belongs to sub-class into base-class the code writed like that

      class CVehicle
      {
      public:
      virtual void fly()
      {
      cout<<"I'm sorry I don't have method fly"<

      2 .we think that sub class has its own method so we should design like that

      class CVehicle
      {
      public:

      };

      class CAeroplane:public CVehicle
      {
      public:
      void fly()
      {
      cout<<"I am a Aeroplane i can fly "<

      so which way will the best way we should order ?

      W Offline
      W Offline
      walterhevedeich
      wrote on last edited by
      #2

      wan.rui@qq.com wrote:

      so which way will the best way we should order ?

      2. A base class should only contain members that are common to those which will derive it.

      Signature construction in progress. Sorry for the inconvenience.

      1 Reply Last reply
      0
      • W wan rui qq com

        today I discuss with my friends .we conculde that there is two ways design base class . 1 .we design the method whether belongs to all class or only belongs to sub-class into base-class the code writed like that

        class CVehicle
        {
        public:
        virtual void fly()
        {
        cout<<"I'm sorry I don't have method fly"<

        2 .we think that sub class has its own method so we should design like that

        class CVehicle
        {
        public:

        };

        class CAeroplane:public CVehicle
        {
        public:
        void fly()
        {
        cout<<"I am a Aeroplane i can fly "<

        so which way will the best way we should order ?

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        The answer is neither. The aim of the base class should be to encapsulate operations that are common to all derived classes. In your first example, having the ability to fly makes no sense in the case of a class like CBike unless you are modelling scenes out of ET. In your second example, there's no reason to have a base class at all (btw, people run, car's don't). A better example would be:

        class CVehicle
        {
        public:
        virtual void Move();
        }

        class CCar: public CVehicle
        {
        public:
        void Move()
        {
        cout << "I am a car. I can be driven" << endl;
        }
        }
        class CBike: public CVehicle
        {
        public:
        void Move()
        {
        cout << "I am a bike. I can be ridden" << endl;
        }
        }

        class CAeroplane: public CVehicle
        {
        public:
        void Move()
        {
        cout << "I am an aeroplane. I can be flown" << endl;
        }
        }

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        W T 3 Replies Last reply
        0
        • P Pete OHanlon

          The answer is neither. The aim of the base class should be to encapsulate operations that are common to all derived classes. In your first example, having the ability to fly makes no sense in the case of a class like CBike unless you are modelling scenes out of ET. In your second example, there's no reason to have a base class at all (btw, people run, car's don't). A better example would be:

          class CVehicle
          {
          public:
          virtual void Move();
          }

          class CCar: public CVehicle
          {
          public:
          void Move()
          {
          cout << "I am a car. I can be driven" << endl;
          }
          }
          class CBike: public CVehicle
          {
          public:
          void Move()
          {
          cout << "I am a bike. I can be ridden" << endl;
          }
          }

          class CAeroplane: public CVehicle
          {
          public:
          void Move()
          {
          cout << "I am an aeroplane. I can be flown" << endl;
          }
          }

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          W Offline
          W Offline
          wan rui qq com
          wrote on last edited by
          #4

          I mean there are some different action in different subclass ,where i can't conclude a common interface like move() . my question is when we at this situation we write all different method in base class?or put the different method in different sub-class?

          B 1 Reply Last reply
          0
          • P Pete OHanlon

            The answer is neither. The aim of the base class should be to encapsulate operations that are common to all derived classes. In your first example, having the ability to fly makes no sense in the case of a class like CBike unless you are modelling scenes out of ET. In your second example, there's no reason to have a base class at all (btw, people run, car's don't). A better example would be:

            class CVehicle
            {
            public:
            virtual void Move();
            }

            class CCar: public CVehicle
            {
            public:
            void Move()
            {
            cout << "I am a car. I can be driven" << endl;
            }
            }
            class CBike: public CVehicle
            {
            public:
            void Move()
            {
            cout << "I am a bike. I can be ridden" << endl;
            }
            }

            class CAeroplane: public CVehicle
            {
            public:
            void Move()
            {
            cout << "I am an aeroplane. I can be flown" << endl;
            }
            }

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            W Offline
            W Offline
            wan rui qq com
            wrote on last edited by
            #5

            and i would like to store subclass with baseclass pointer.maybe we can dynamic_cast it to subclass and then to visit his method?or we could use base class pointer to visist method.

            1 Reply Last reply
            0
            • P Pete OHanlon

              The answer is neither. The aim of the base class should be to encapsulate operations that are common to all derived classes. In your first example, having the ability to fly makes no sense in the case of a class like CBike unless you are modelling scenes out of ET. In your second example, there's no reason to have a base class at all (btw, people run, car's don't). A better example would be:

              class CVehicle
              {
              public:
              virtual void Move();
              }

              class CCar: public CVehicle
              {
              public:
              void Move()
              {
              cout << "I am a car. I can be driven" << endl;
              }
              }
              class CBike: public CVehicle
              {
              public:
              void Move()
              {
              cout << "I am a bike. I can be ridden" << endl;
              }
              }

              class CAeroplane: public CVehicle
              {
              public:
              void Move()
              {
              cout << "I am an aeroplane. I can be flown" << endl;
              }
              }

              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              T Offline
              T Offline
              TorstenH
              wrote on last edited by
              #6

              +5. That solution is working safe.

              regards Torsten When I'm not working

              1 Reply Last reply
              0
              • W wan rui qq com

                I mean there are some different action in different subclass ,where i can't conclude a common interface like move() . my question is when we at this situation we write all different method in base class?or put the different method in different sub-class?

                B Offline
                B Offline
                BobJanova
                wrote on last edited by
                #7

                If you can't ask a common question, how can you interact with the base class? Answer: you can't. If you want something to move, no further questions asked, provide a move() method (possibly abstract/pure virtual) and override it to do the actual movement in subclasses. If you need to know the type of something before asking it the question, then you're probably asking the wrong question.

                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