Hello,
This is the problem: I get the right function call only from the 3rd derivated class, when the other twos are called are not.
Source :

#include<iostream.h>
#include<conio.h>
#include<string.h>

class IV{
	char *loc;
public:
	IV(){loc=NULL;}
	IV(char *_loc){
		loc=new char [strlen(_loc)+1];
		strcpy(loc,_loc);
	}
	~IV(){
		if(loc) delete [] loc;
		loc=NULL;
	}
	void setloc(char *_loc){
		loc=new char [strlen(_loc)+1];
		strcpy(loc,_loc);
	}
	char *getloc(){
		return loc;
	}
	virtual void print(){
		cout <<"Descrierea IV"<< endl;
	}

};
class IVP:public IV{
	char *name;
public:
	IVP(){name=NULL;}
	IVP(char *_name,char *_loc):IV(_loc){
		name=new char [strlen(_name)+1];
		strcpy(name,_name);
	}
	~IVP(){
		if(name) delete [] name;
		name=NULL;
	}
	void setname(char *_name){
		name=new char [strlen(_name)+1];
		strcpy(name,_name);
	}
	char *getname(){
		return name;
	}
	 void print(IVP *iv){
		cout << "Descrierea IVP" << endl;
	}

};
class IVM: public IV{
	char *name;
public:
	IVM(){}
	IVM(char *_name,char *_loc):IV(_loc){
		name=new char [strlen(_name)+1];
		strcpy(name,_name);
	}
	~IVM(){
		if(name) delete [] name;
		name=NULL;
	}
	void setname(char *_name){
		name=new char [strlen(_name)+1];
		strcpy(name,_name);
	}
	char *getname(){
		return name;
	}
	 void print(IVP *iv){
		cout << "Descrierea IVM" << endl;
	}

};
class IVS: public IV{
	char *name;
public:
	IVS(){}
	IVS(char *_name,char *_loc):IV(_loc){
		name=new char [strlen(_name)+1];
		strcpy(name,_name);
	}
	~IVS(){
		if(name) delete [] name;
		name=NULL;
	}
	void setname(char *_name){
		name=new char [strlen(_name)+1];
		strcpy(name,_name);
	}
	char *getname(){
		return name;
	}
	 void print(){
		cout << "Descrierea IVS" << endl;
	}

};

void main(){
clrscr();
IV *iv[3];
IVP ivp("Guguta","Balti");
IVM ivm("Columna","Chisinau");
IVS ivs("UTM","Chisinau");
iv[0]=&ivp;
iv[1]=&ivm;
iv[2]=&ivs;
for(int i=0;i<3;i++){
	iv[i]->print();
}

getch();
}

THIS IS THE OUTPUT:
Descrierea IV
Descrierea IV
Descrierea IVS
EXPECTED OUTPUT:
Descrierea IVP
Descrierea IVM
Descrierea IVS

Recommended Answers

All 8 Replies

It's <iostream> not <iostream.h>, try <string> not the old C way.
By using string you can simplify it and create an easier to follow flow of code. http://www.cplusplus.com/reference/string/string/

And by the way, main() must return zero.

I'm sorry, but wiping out ".h" just gives me a ton of errors.
Also, i have not been thought about the string object yet. I'm on 2nd year at the university.
Would you be so kind and offer more help on my issue?
Are there any syntactical errors concerning polymorphism?
Btw, i'm using Turbo C++ 3.0 .
Thank you.

uhmmm.... Get a compiler from this century. Strings are a million times easier than what you're doing now.

uhmmm.... Get a compiler from this century. Strings are a million times easier than what you're doing now.

Listen that is not serious response. What i would like to know is if my code contains errors, or is this just the 'ancient turbo c' compiler?
If the problem is in my coding then pls direct me to it.
Can you try running the program on your 'this-century' compiler? and see the output?
(Or anyone else, please.)

Uhm, one other thing... i have no idea of the string object you where trying to tell me. I do appreciate it however, but that will not solve my problem. It will only make it bigger for now(because i'll have to learn the string thingy instead of understanding the poly*ism principle. )
That program is a lab assignment, and i need to understand better polymorphism.

First off the error that you have made is that in C++

void print(char*);
void print();  

are two different functions/methods.

Thus you HAVE NOT defined a print() method in class IVM.
You defined some weird function called void print(IVP* iv) and that is not void print()

That is why the program does not call it.

Second, the huge number of errors/warning from using istream, cstdlib and cstring etc in the include are because the compiler is trying to help you write better code.

I strongly suggest that you get a copy of gcc. Code your assignments in that and it will help a lot more. (gcc is free). If you are sticking with windows there are several other free compilers as well, but gcc is so universal and very very picky about writing to the standard. (something that university professors can get very mark happy about!)

Thank you, that solved it! Those where 'leftovers' from trying polymorphism adhoc. I didn't noticed!

I strongly suggest that you get a copy of gcc.

I am running a Linux system, it has gcc ...
But it's horrible when trying to debug because i use it via the Terminal.
is there a real IDE GCC compiler for linux(Ubuntu)?

If you are using linux, there are a few tricks to make life MUCH better.
I am assuming that by debugging you mean the error messages that gcc/g++ give you when you compile your code. [Not, how to debug you code are runtime, which gcc facilitates but doesn't do]

Ok first there are IDE's for linux (e.g. eclipse). I don't use them since I hate being confined to so small a number of views of the code and I like emacs/gcc.

Second: Learn to use make. A simple make file for each project is a massive saving in time. [and vim/emacs both support building from it, if that is how you want to work]

Now if you are using gcc native. There are some ways to make life better.

(a) Know that you can pipe the errors to a file e.g. g++ file.cpp 2> error.log . The advantage of this is that you can the edit the file and I use vim to edit the error file.
[along with a :set nowrap] and everything looks lots better.

(b) gcc gives LOTS of warnings. Be prepared and build up you code in small fragments and REMOVE all the warnings by improving your code.

(c) Use g++ -g and don't do -O or -O2 since you want to debug it easily, while developing.

(d) ddd is your debugging friend!! Compile with g++ -g and no optimization and the ddd program.

Finally, the best thing about using gcc from the terminal is the shear simplicity of it. It is not in my way when it is not compiling. It just does the job and does it very very well. It gives very full error messages (that I do accept, overwhelm the newbie) but time and time again it pays me back by pointing out the route by which the problem occurred. That is often essential in figuring out the error, particularly in template coding.

Finally, once you get over the learning curve, gcc /terminal /editor/debugger, is a combination that just doesn't change. The same stuff that worked 15 years ago still works. That cannot be said about the IDE's. Yes the compiler changes, even the languages, but the method of working doesn't.

All of the above is personnel opinion and I am happy coding with gcc.
You are going to have to find what works for you. But whatever it is learn it and learn it well, know the shortcuts , the tricks, the optimizations, everything.

Thank you.
Though this will take time , trying to learn the make thing and all else you have said. But still, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.