C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 25th, 2012, 03:32 PM
icelemon icelemon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 8 icelemon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 50 m 18 sec
Reputation Power: 0
Red face C++ Class (composition and inheritance)

I have written 4 header files (of classes) of which 3 classes has been successfully compiled (Customer, GoldCustomer and PlatinumCustomer). GoldCustomer and PlatinumCustomer are derivation (inheritance) of Customer while the problematic MovieTicket class is a composition of all of the other 3 classes. This is to follow the requirement imposed : to show inheritance and composition.

Compiling MovieTicket gives me these errors. Can anyone help to examine and explain? It seems like there are repetition of declaration of the classes (but from what I read in Google, as long as you put include the classes in the header files and put the header guard, that error wouldnt come out). One other thing is, my array of classes gives errors...I have no idea why

Code:
In file included from class_GoldCustomer.hpp:3:0, from class_MovieTicket.hpp:5:
class_Customer.hpp:11:7: error: redefinition of ‘class Customer’

In file included from class_MovieTicket.hpp:4:0: class_Customer.hpp:11:7: error: previous definition of ‘class Customer’

In file included from class_PlatinumCustomer.hpp:3:0, from class_MovieTicket.hpp:6:
class_Customer.hpp:11:7: error: redefinition of ‘class Customer’

In file included from class_MovieTicket.hpp:4:0: class_Customer.hpp:11:7: error: previous definition of ‘class Customer’

class_MovieTicket.hpp:29:22: error: expected ‘]’ before ‘;’ token
class_MovieTicket.hpp:29:25: error: expected unqualified-id before ‘]’ token
class_MovieTicket.hpp:32:26: error: expected ‘]’ before ‘;’ token
class_MovieTicket.hpp:32:29: error: expected unqualified-id before ‘]’ token
class_MovieTicket.hpp: In member function ‘void MovieTicket::newMovie(int, std::string, std::string, int, int, float)’:
class_MovieTicket.hpp:53:3: error: ‘ListGold’ was not declared in this scope
class_MovieTicket.hpp:53:12: error: expected ‘]’ before ‘;’ token
class_MovieTicket.hpp:53:15: error: expected primary-expression before ‘]’ token
class_MovieTicket.hpp:53:15: error: expected ‘;’ before ‘]’ token
class_MovieTicket.hpp:54:3: error: ‘ListPlat’ was not declared in this scope
class_MovieTicket.hpp:54:12: error: expected ‘]’ before ‘;’ token
class_MovieTicket.hpp:54:15: error: expected primary-expression before ‘]’ token
class_MovieTicket.hpp:54:15: error: expected ‘;’ before ‘]’ token
class_MovieTicket.hpp: In member function ‘void MovieTicket::Purchase()’:
class_MovieTicket.hpp:103:4: error: ‘ListGold’ was not declared in this scope
class_MovieTicket.hpp:108:4: error: ‘ListPlat’ was not declared in this scope
Compilation failed.


Below are the codes:

class_Customer.hpp
Code:
#include <iostream>
#include <string>

//header guard
#ifndef CUSTOMER_H
#define CUSTOMER_H

using namespace std; 

class Customer {

protected:
	//data members
	int id;
	string type;
	int sold;

//public:
	//constructor and destructor
	Customer(){
		type = "non member"; //default this customer class as 'non member'
		sold = 0;
	}
	~Customer(){}
	
	//functions member
	void setID(int inputID){ //set ID of the customer
		id = inputID;
	}
	
	int getSold(){ //get no. of sold
		return sold;
	}
	
};
#endif


class_GoldCustomer.hpp
Code:
#include <iostream>
#include <string>
#include "class_Customer.hpp"

using namespace std; 

//header guard
#ifndef GOLDCUSTOMER_H
#define GOLDCUSTOMER_H


class GoldMember : protected Customer{

private:
	//data members
	int gold_id;
	int annual_fee;
	float discount;
	
public:
	//constructor and destructor
	GoldMember(){
		type = "gold"; 
		sold = 0;
		annual_fee = 10;
		discount = 0.1;
	}
	~GoldMember(){}
	
	//functions member
	int getAnnualFee(){ //get annual fee
		return annual_fee;
	}
	
	float getDiscount(){ //get discount
		return discount;
	}

};
#endif


class_PlatinumCustomer.hpp
Code:
#include <iostream>
#include <string>
#include "class_Customer.hpp"

#define POINT 5; //how many ticket purchased needed to eligible for one free ticket

using namespace std; 

//header guard
#ifndef PLATINUMCUSTOMER_H
#define PLATINUMCUSTOMER_H

class PlatinumMember : protected Customer{

private:
	//data members
	int plat_id;
	int annual_fee;
	float discount;
	int redeem_point = 0; //count the tickets purchased as 'points'
	
public:
	//constructor and destructor
	PlatinumMember(){
		type = "gold"; 
		sold = 0;
		annual_fee = 50;
		discount = 0.2;
		redeem_point = 0;
	}
	~PlatinumMember(){}
	
	//functions member
	int getAnnualFee(){ //get annual fee
		return annual_fee;
	}
	
	float getDiscount(){ //get discount
		return discount;
	}
	
	int getFRedeemPoint(){ //get redeem point
		return redeem_point;
	}
	
	int getFreeTicket(){ //get no. of free tickets based on redeem point
		int result;
		result = redeem_point/POINT;
		return result;
	}

};
#endif


Finally, failed to compile:

class_MovieTicket.hpp
Code:
#include <iostream>
#include <string>

#include "class_Customer.hpp"
#include "class_GoldCustomer.hpp"
#include "class_PlatinumCustomer.hpp"

//header guard
#ifndef MOVIETICKET_H
#define MOVIETICKET_H


using namespace std; 

#define MAX 20; //no of tickets per movie hall available

class MovieTicket {
	
private:
	int movie_id;
	string movie_title;
	string date;
	int hall_no;
	int seat_no;
	float ori_price;
	
	int NonMemberCount;

	GoldMember ListGold[MAX];
	int GoldMemberCount;
	
	PlatinumMember ListPlat[MAX];
	int PlatMemberCount;
	
	static int AvailTicket;
	static int SoldTicket;

public:
	//constructor and destructor
	MovieTicket(){};
	~MovieTicket(){};
	
	//functions member
	void newMovie(int inputMovieID, string inputTitle, string inputDate, int inputHall, int inputSeat, float inputPrice){
		
		movie_id = inputMovieID;
		movie_title = inputTitle;
		date = inputDate;
		hall_no = inputHall;
		seat_no = inputSeat;
		ori_price = inputPrice;
		
		ListGold[MAX] = {0};
		ListPlat[MAX] = {0};
	
		AvailTicket = MAX;
		SoldTicket = 0;
	}
	
	int getAvailTicket(){
		return AvailTicket;
	}
	
	int getSoldTicket(){
		return SoldTicket;
	}
	
	void Purchase(){
		int no_of_ticket;
		int membership;
		int Inputgold_id;
		int Inputplat_id;
		
		cout <<"Enter Movie ID?" << endl;
		cin >> movie_id;
		cout << endl << "How many ticket?" << endl;
		cin >> no_of_ticket;
		cout << endl << "Enter membership? 1. Non 2. Gold 3.Plat" << endl;
		cin >> membership;
		
		this->SoldTicket += no_of_ticket;
		this->AvailTicket = MAX - this->SoldTicket;
		
		
		if (membership == 2){
			cout << "Enter your Gold Membership ID?" << endl;
			cin >> Inputgold_id;
			GoldMember Inputgold_id; //create a Gold customer class with 'ID' as the name
		} 
		else if (membership == 3){
			cout << "Enter your Platinum Membership ID?" << endl;
			cin >> Inputplat_id;
			PlatinumMember Inputplat_id; //create a Plat customer class with 'ID' as the name
		}
		
		switch (membership){
			case 1:
			NonMemberCount++;
			break;
			
			case 2:
			GoldMemberCount++;
			ListGold[GoldMemberCount] = Inputgold_id;
			break;
			
			case 3:
			PlatMemberCount++;
			ListPlat[PlatMemberCount] = Inputplat_id;
			break;
		}
	}
};
#endif


p/s: sorry for the long post.

Reply With Quote
  #2  
Old December 25th, 2012, 03:43 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,839 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 18 h 45 m 47 sec
Reputation Power: 1774
A couple of points.

1. #define MAX 20; //no of tickets per movie hall available
#defines do not normally have ; in them.

Otherwise, your array expands to
GoldMember ListGold[20;];

Besides, this is C++, so you can do
const int MAX = 20;

Secondly, do NOT put
using namespace std;
in your header files.

If someone using your header files doesn't want the std namespace, and you go and use it anyway, then there is no way back for your library user.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old December 25th, 2012, 04:04 PM
icelemon icelemon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 8 icelemon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 50 m 18 sec
Reputation Power: 0
Quote:
Originally Posted by salem
A couple of points.

1. #define MAX 20; //no of tickets per movie hall available
#defines do not normally have ; in them.

Otherwise, your array expands to
GoldMember ListGold[20;];

Besides, this is C++, so you can do
const int MAX = 20;

Secondly, do NOT put
using namespace std;
in your header files.

If someone using your header files doesn't want the std namespace, and you go and use it anyway, then there is no way back for your library user.


1. Ahh You were right. I miss-put that ";". Thanks.

2. I removed "using namespace std" from all header files except the class_Customer.h since it produces errors when it is compiled. Somehow, it doesnt recognized <string> header files without it.

Code:
class_Customer.hpp:15:2: error: ‘string’ does not name a type
class_Customer.hpp: In constructor ‘Customer::Customer()’:
class_Customer.hpp:21:3: error: ‘type’ was not declared in this scope
Compilation failed.


3. However, the main issue of "repetition of declaration of the classes " is still unknown..I'm still reading and tweaking around though to no avail

Reply With Quote
  #4  
Old December 25th, 2012, 05:04 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,372 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 39 m 38 sec
Reputation Power: 383
I'm quite sure you need
std::string
where you have string. I don't mind typing some to resolve namespace. You can write in m4 if this is too wordy for you.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #5  
Old December 25th, 2012, 05:48 PM
icelemon icelemon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 8 icelemon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 50 m 18 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
I'm quite sure you need
std::string
where you have string. I don't mind typing some to resolve namespace. You can write in m4 if this is too wordy for you.


You are right. I take your points, thanks

btw, I have solved the "repetition of declaration of the classes" issues. I should have put the header guard before everything else.

Some errors remain afterwards but I think that will do for now, thanks.

I still could take any more good tips though. Thanks

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C++ Class (composition and inheritance)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap