Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder syco__'s Avatar
    Join Date
    Oct 2005
    Location
    Australia
    Posts
    194
    Thanks
    41
    Thanked 4 Times in 4 Posts

    Unhappy c++: virtual constructor

    Hey guys i have a couple of files going

    I have 2 .cpp files
    2 .h files

    and im trying to set up some sort of inheritance where i use the objects from the first but then i have a name double up and i want to use virtual so it knows which one i want to use but i am having some trouble understanding how this works and would really like some help.

    I have tried just straight out typing virtual in front of the one i want to use but i get no change to my output. Im feeling i need to use a pointer but i cant seem to find any info on my direct problem or maybe i am just googleling the wrong keywords.

    I havnt posted any code cause i want to understand the concept before i get help on the told.

    Thanks any help would be great.
    .pLeAd InSaNiTy.

  • #2
    Regular Coder syco__'s Avatar
    Join Date
    Oct 2005
    Location
    Australia
    Posts
    194
    Thanks
    41
    Thanked 4 Times in 4 Posts
    Alright so after playing my code my code actually works well but i was under the impression that if i chucked virtual in front of the one i didnt want to use then it would use the other which is not the case at all because it has already been told which one to use when the member(not sure if thats the right word) was created.

    If this is the case why use the word virtual at all, in what instance would the call not know which one it would need to use?

    Thanks, sorry my c++ lingo is not up to scratch so this will make my questioning alot harder. but please feel free to have a stab at answering regardless.

    Thanks
    .pLeAd InSaNiTy.

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    I'm not following what you are asking. Perhaps if you show some of your code that will clear it up. Object constructors can't be virtual though.
    OracleGuy

  • #4
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    You have got yourself confused. If you are wanting to make something virtual you should you place the virtual in front of the base class function and the derived class that inhierts it will us the function of its class. To make it pure virtual you need to set the function equal to zero, making the base class abstract.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #5
    Regular Coder syco__'s Avatar
    Join Date
    Oct 2005
    Location
    Australia
    Posts
    194
    Thanks
    41
    Thanked 4 Times in 4 Posts
    i don't need it to be pure as i still need to use the base class version of it on other calls as i only have the base call and one derived class. The way i see my code it is working as i have taken out the derived class version and it runs the base calls version but i just having trouble understanding what the actually virtual keyword does as having it in there doesnt make any difference. But maybe i have done something wrong.

    Code:
    car.h
    class Car {
    
    	public: 
    		Car();
    		Car(int n);
    		string another_thing()const;
    			
    	private:
    		int n;
    };
    Code:
    Car.cpp
    
    Car::another_thing() const{
           cout << "something" << endl;
    }
    Code:
    bus.h
    
    class Roman: public Number {
    
    	public: 
    		string get_fuel_amount()const;
    			
    	private:
    		
    		int n;
    };
    Code:
    bus.cpp
    
    Bus::another_thing() const{
           
    Car::another_thing() const{
           cout << "something different" << endl;
    }
    }
    Code:
    main.cpp
    
    int main(){
    Car one;
    one.another_thing() << "\n";
    Car two
    two.another_thing() << "\n";
    Bus three
    three.another_thing() << "\n";
    return 0;
    };
    That is my code dumbed down if there is errors in the code it doesnt matter just trying to get the concept right about the virtual. As you can see i have not set virtual, yet is always knows what type it is. I'm just to work out where and why we set it and why its not always needed but mainly to when and why i would use it.

    Thanks for taking the time. much appreciated.
    .pLeAd InSaNiTy.

  • #6
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    Here let me give you an example.
    Code:
    car.h
    #ifndef CAR_H
    #define CAR_H
    class Car {
     
        public: 
    Car(int number)
    {
      n = number;
    }
    ~Car()
    {
    }
    virtual std::string another_thing()
    {
        cout<<"This car class instance has "<<n<<" things.";
    }   
        protected:
     int n;
    };
    #endif
    Code:
    bus.h
    #ifndef BUS_H
    #define BUS_H
     
    class Bus: public Car
    {
      private: 
       int x;
      public:
       Bus(int xnumbers, int numbers):
         x(xnumbers),
         Car(numbers)
       {
        }
      virtual std::string another_thing()
    {
      cout<<"This bus class instance has "<<n<<" number of things";
      cout<<" and has "<<x<<" number of xthings.";
    }
     
    }
    #endif
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •