Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-28-2012, 02:51 PM   PM User | #1
syco__
Regular Coder

 
syco__'s Avatar
 
Join Date: Oct 2005
Location: Australia
Posts: 178
Thanks: 39
Thanked 3 Times in 3 Posts
syco__ has a little shameless behaviour in the past
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.
syco__ is offline   Reply With Quote
Old 04-28-2012, 04:15 PM   PM User | #2
syco__
Regular Coder

 
syco__'s Avatar
 
Join Date: Oct 2005
Location: Australia
Posts: 178
Thanks: 39
Thanked 3 Times in 3 Posts
syco__ has a little shameless behaviour in the past
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.
syco__ is offline   Reply With Quote
Old 04-28-2012, 05:50 PM   PM User | #3
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
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
oracleguy is offline   Reply With Quote
Old 04-28-2012, 10:18 PM   PM User | #4
Chris Hick
Regular Coder

 
Join Date: Oct 2010
Location: Florence, MS
Posts: 476
Thanks: 10
Thanked 33 Times in 32 Posts
Chris Hick is an unknown quantity at this point
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. ^_^
Chris Hick is offline   Reply With Quote
Old 04-29-2012, 01:27 AM   PM User | #5
syco__
Regular Coder

 
syco__'s Avatar
 
Join Date: Oct 2005
Location: Australia
Posts: 178
Thanks: 39
Thanked 3 Times in 3 Posts
syco__ has a little shameless behaviour in the past
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.
syco__ is offline   Reply With Quote
Old 04-29-2012, 06:50 PM   PM User | #6
Chris Hick
Regular Coder

 
Join Date: Oct 2010
Location: Florence, MS
Posts: 476
Thanks: 10
Thanked 33 Times in 32 Posts
Chris Hick is an unknown quantity at this point
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. ^_^
Chris Hick is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:36 AM.


Advertisement
Log in to turn off these ads.