Hey everyone, I haven't been here in sometime but I'm in need of some guidance. One of the questions I have is how do I enter the name, length, and time for each course? Thanks in advance for the help. Here's what I'm supposed to do:

Using OOp inheritance and polymorphism, create four classes as follows:

(1) CSci_Course:
-an abstract class with virtual function getClassTime(); Reason: it is meaningless to have a real classTime for a general CSci class.
-with variables classTime, length, className;

(2) CSci_41:
-an implementation of function getClassTime();
-with variable classTime, className;
-Constructor with arguments of classTime, length, className.

(3) CSci_148:
-an implementation of function getClassTime();
-with variable classTime, className;
-Constructor with arguments of classTime, length, className.

(4) CSci_156:
-an implementation of function getClassTime();
-with variable classTime, className;
-Constructor with arguments of classTime, length, className.

The main program asks the starting time, length, and name of each class. Then, creates 3 pointers of type Csci-Course, and calls getClassTime() function. It should return the actual time of different courses. Finally, if there is a conflict on time schedule, a warning should be provided with clear message which two courses have a conflict.

Note:
(1) you must use Polymorphism;
(2) Make an individual file for each class and the main function;
(3) main function should be put in a file "schedule.cc".

Here's what I have so far:

#include <iostream>
#include <string>

using namespace std;

class CSci_Course
{

public:
 
	CSci_Course();
	virtual int getClassTime() = 0;

protected:
     
	string className;
	int classTime;
	int classLength;
};

class CSci_41: public CSci_Course
{

public:

	CSci_41(string name, int time, int length) : CSci_Course()
	{
		className = name;
		classTime = time;
		classLength = length;
	}

	void setClassName( string newClassName )
	{
		className = newClassName;
	}
	
	string getClassName()
	{
		return className;
	}
	
	void setClassTime( int newClassTime )
	{
		classTime = newClassTime;
	}
	
	int getClassTime()
	{
		return classTime;
}
	
	void setClassLength( int newClassLength )
	{
		classLength = newClassLength;
	}
	
	int getClassLength()
	{
		return classLength;
	}

};

class CSci_148: public CSci_Course
{

public:

	CSci_148( string name, int time, int length )
	{
		className = name;
		classTime = time;
		classLength = length;
	}
	
	void setClassName( string newClassName2 )
	{
		className = newClassName2;
	}

	string getClassName()
	{
		return className;
	}
	
	void setClassTime( int newClassTime )
	{
		classTime = newClassTime;
	}
	
	int getClassTime()
	{
		return classTime;
	}

	void setClassLength( int newClassLength )
	{
		classLength = newClassLength;
	}

	int getClassLength()
	{
		return classLength;
	}
};

class CSci_156 : public CSci_Course
{

public:

	CSci_156( string name, int time, int length )
	{
		className = name;
		classTime = time;
		classLength = length;
	}

	int getClassTime()
	{
		return classTime;
	}

	string getClassName()
	{
		return className;
	}

	int getClassLength()
	{
		return classLength;
	}
};

int main()
{
	CSci_41 C41( "CSCI 41",1100,50 );
	CSci_148 C148( "CSCI 148", 1500, 50 );
	CSci_156 C156( "CSCI 148", 1500, 50 );

	CSci_Course *C1 = &C41;
	CSci_Course *C2 = &C148;
	CSci_Course *C3 = &C156;
	
	cout << C1->getClassTime();
	cout << C1->getClassName();
	cout << C1->getClassLength();
   
	   
	return 0;
}

Recommended Answers

All 10 Replies

how do I enter the name, length, and time for each course?

That's the only question? i find it strange that you could write a polymorphic code, use virtual functions but find it difficult to get the input!!! arn't those the values you are passing in the ctors?

Thanks for the reply. I spoke with my professor frequently while writing the polymorphic code and he instructed the class on the implementation of the virtual functions. I feel that I have a poor understanding of polymorphism. Right now I'm just trying to limp through the assignment then hopefully reread the chapter to gain a better understanding. However, when I do enter the input would I just do:

cout << "enter a class name: ";
   string newClassName;
   getline( cin, newClassName );
   C41.setClassName( newClassName );

Would I repeat that for each class? Thanks again for the reply.

Since its a beginner assignment i guess that's how you are supposed to take the inputs. i dont think you have too many inputs so this shouldn't be too time consuming.

It just looked cluttered, so I felt I was doing something wrong. Asking for the class name 3 times, length 3 times, and time 3 times. So that is what I'm supposed to do? Also, if you could possibly tell me why the compiler says that 'getClassName' and'getClassLength' are not members of CSci_Course. Thanks again for the help.

Because they are not!!!.. In the class definition of CSci_Course you have not declared these functions.

lol.....I'm tired....I just saw that they were not declared in the class CSci_Course. However, I'm now getting two errors and I have no clue what's wrong. I'm getting:
1>project2.obj : error LNK2019: unresolved external symbol "public: __thiscall CSci_Course::CSci_Course(void)" (??0CSci_Course@@QAE@XZ) referenced in function "public: __thiscall CSci_41::CSci_41(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,int)" (??0CSci_41@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HH@Z)

and

fatal error LNK1120: 1 unresolved externals

If you could help me with these two errors I think I'll be done for the night. Thank again

Hey chandra.rajat, I wanted to let you know that I just figured out what I was doing wrong. Thanks again for all your help.

have you put everything in 1 file or separate files?

CSci_41(string name, int time, int length) : CSci_Course()

you dont need to explicitly call the base default ctor. remove the call and try.

I'm putting everything into files now and it's giving me a bit of trouble. It's telling me that one of my files does not exist or there's no such directory. I'm not sure why it says that. I have everything saved in the same folder.

can you post the include statements?

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.