Polymorphism

Hı guys.I have problem with a example which is in Big C ++.I don^t execute this example.When I uses virtual keyword,the program don^t execute.When i don't use virtual keywords, program execute wrongly.Travelclock class is derived from clock class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//clock3.cpp (other cpp files are added to project.
#include<iostream>
#include<iomanip>
#include<vector>
#include<conio.h>

using namespace std;
#include "travelclock.h"

int main()
{
	vector<Clock*> clocks(3);
	clocks[0]=new Clock(true);
	clocks[1]=new TravelClock(true, "rome", 9);
	clocks[2]=new TravelClock(false, "tokyo", -9);
	
	for (int i=0; i < clocks.size(); i++)
	{
		cout <<clocks[i]->get_location() << "time is "
			 <<clocks[i]->get_hours() << ":"
			 <<setw(2) << setfill('0')
			 <<clocks[i]->get_minutes()
			 <<setfill(' ') << "\n";
	}
	getch();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//travelclock.h
#include<string>
using namespace std;

#include "clock.h"

class TravelClock : public Clock
{
public:
	TravelClock(bool mil,string loc, int diff);
	 string get_location() const;
	 int get_hours() const;
private:
	string location;
	int time_difference;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//clock.h
#ifndef CLOCK_H
#define CLOCK_H

#include <string>

using namespace std;

class Clock
{
public:
		Clock(bool use_military);

		virtual string get_location() const;

		virtual int get_hours() const;

		int get_minutes() const;

		bool is_military() const;

		
private:
	bool military;
};
#endif



Last edited on
What do you mean by "program don^t execute"? Exact error messages, please.
LINK : C:\Users\ugur\Documents\Visual Studio 2010\Projects\polymorphism\Debug\polymorphism.exe not found or not built by the last incremental link; performing full link
1>clock3.obj : error LNK2019: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Clock::get_location(void)const " (?get_location@Clock@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main
1>clock3.obj : error LNK2019: unresolved external symbol "public: int __thiscall Clock::get_hours(void)const " (?get_hours@Clock@@QBEHXZ) referenced in function _main
1>C:\Users\ugur\Documents\Visual Studio 2010\Projects\polymorphism\Debug\polymorphism.exe : fatal error LNK1120: 2 unresolved externals
Those are linker errors, from last stage of compilation.

1. Implementation for Clock::get_location() is not within the object files that are included in linking.

2. Implementation for Clock::get_hours() is not within the object files that are included in linking.

Where are those function implementations?
I added all cpp and headers files to projects.I use visual studio



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//travelclock.cpp

#include "travelclock.h"

TravelClock::TravelClock(bool mil, string loc, int diff)
	:Clock(mil)
{
	location=loc;
	time_difference=diff;
	while (time_difference <0)
		time_difference=time_difference + 24;
}
string TravelClock::get_location() const
{
	return location;
}
int TravelClock::get_hours() const
{
int h = Clock::get_hours();
if(is_military())
	return (h +time_difference)  %24;
else
{
	h=(h + time_difference) %12;
	if (h==0) return 12;
	else return h;
}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//clock.cpp

#include "clock.h"
#include "ccc_time.h"

Clock::Clock(bool use_military)
{
	military=use_military;
}
string Clock::get_location() const
{
	return "Local";
}
int Clock::get_hours() const
{
	Time now;
	int hours=now.get_hours();
	if (military) return hours;
	if (hours==0) 
		return 12;
	else if (hours>12)
		return hours -12;
	else
		return hours;
}
int Clock::get_minutes() const
{
	Time now;
	return now.get_minutes();
}
bool Clock::is_military() const
{
	return military;
}

thanks you for helping keskiverto.I solved the problem.I reoved cpp and headers filesfrom the projects and I added them again and the problem is solved.
Last edited on
Topic archived. No new replies allowed.