inheritance.....stuck in overloading operator<<

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// assngment5 inheritance fedx.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

struct customer
{
	string name;
	string adress;
	string state;
	string city;
	int zip;
};

class Pakage{
public :
	Pakage (){};
	customer customers,recipient;
	Pakage(unsigned a,unsigned b){weight=0;cost=0;}
	void set(unsigned a,unsigned b){weight =a;cost =b;}
	unsigned get_weight(){return weight;}
	unsigned get_cost(){return weight;}
	unsigned calcCost(){return weight*cost;}
	friend ostream& operator >>(ostream&,Pakage&); 
protected:
	unsigned weight,cost;
};

class Two_day_pakage:public Pakage{
protected:
	unsigned flat_fee;
public:
	Two_day_pakage(){};
	Two_day_pakage(unsigned a,unsigned b,unsigned c):Pakage(b,c) {flat_fee=a=0;}
	void set_flatFee(int a){flat_fee=a;}
	double get_flatFee(){return flat_fee;}
	double calcCost(){return flat_fee+(weight*cost);}
	//friend ostream& operator <<(ostream&,Two_day_pakage&);
};

class Over_night_pakage:public Pakage
{
protected:
	unsigned additional_fee;
public:
	Over_night_pakage(){};
	Over_night_pakage(unsigned a,unsigned b,unsigned c):Pakage(b,c){additional_fee=a=0;}
	void set_additionalFee(int a){additional_fee=a;}
	double get_additionalFee(){return additional_fee;}
	double calcCost(){return (additional_fee*weight)+(weight*cost);}
	//friend istream& operator >>(istream&,Over_night_pakage&);
};
ostream& operator <<(ostream in,Pakage& A)
{
	cout<<"please enter the details of customer:\n";
	in<<"the name of the customer :\n";
	in<<A.customers.name;
	cout<<"the adress of the customer :\n";
	in <<A.customers.adress;
	cout<<"the city of the customer :\n";
	in<<A.customers.city;
	cout<<"the state of the customer :\n";
	in<<A.customers.state;
	cout<<"the zip code of your city:\n";
	in<<A.customers.zip;
	//****************************************************************

	cout<<"the details of recipient:\n";
	cout<<"the name of the recipient :\n";
	in<<A.recipient.name;
	cout<<"the adress of the recipient :\n";
	in <<A.recipient.adress;
	cout<<"the city of the recipient:\n";
	in<<A.recipient.city;
	cout<<"the state of the recipient :\n";
	in<<A.recipient.state;
	cout<<"the zip code of your recipient:\n";
	in<<A.recipient.zip;


	/*cout<<"please enter the weight of your pakage:\n";
	in>>A.weight;
	cout<<"please enter the cost per unit weight:\n";
	in>>A.cost;
	*/
	return in;
};
/*
istream& operator >>(istream in,Two_day_pakage& A)
{
	cout<<"please enter the flat fee per unit wieght:\n";
	in>>A.flat_fee;
}
*/
int main()
{

	Pakage A;
	unsigned weight,cost,flatFee,additionalFee;
	cout<<"please enter the weight of your pakage\n";
	cin>>weight;
	cout<<"please enter the cost per unit weight of your pakage\n";
	cin>>cost;
	A.set(weight,cost);
	
	cout<<"please enter the details of customer:\n";
	cout<<"enter the name of the customer :\n";
	cin>>A.customers.name;
	cout<<"enter the adress of the customer :\n";
	cin >>A.customers.adress;
	cout<<"enter the city of the customer :\n";
	cin>>A.customers.city;
	cout<<"enter the state of the customer :\n";
	cin>>A.customers.state;
	cout<<"enter the zip code of your city:\n";
	cin>>A.customers.zip;
	
	cout<<"please enter the details of recipient:\n";
	cout<<"enter the name of the recipient :\n";
	cin>>A.recipient.name;
	cout<<"enter the adress of the recipient :\n";
	cin >>A.recipient.adress;
	cout<<"enter the city of the recipient:\n";
	cin>>A.recipient.city;
	cout<<"enter the state of the recipient :\n";
	cin>>A.recipient.state;
	cout<<"enter the zip code of your recipient:\n";
	cin>>A.recipient.zip;
	
	
	Two_day_pakage B;
	//cout<<"please enter the flat fee for Two day pakage \n";
	flatFee=150;
	B.set_flatFee(flatFee);
	cout<<"the total cost of two day pakage is :";
	double day_pakage=B.calcCost();
	cout<<B.calcCost();
	//cout<<day_pakage;
	additionalFee=100;
	Over_night_pakage C;
	C.set_additionalFee(additionalFee);
	cout<<"\nthe cost for over night pagkage is :";
	cout<<C.calcCost();
	cout<<"do you want to display the information of customer and recipient ??\n if yes press Y else press any key to exit";
	char a;
	cin>>a;
	if(a=='Y'||a=='y')
		cout<<A;
	getch();
	return 0;
}


i am having pblm in displaying the oblject A
1
2
if(a=='Y'||a=='y')
		cout<<A;

and also having pblm in geting the correct answer from overRided functions calcCost
ostream& operator <<(ostream in,Pakage& A)

You must pass in by reference. (And I'd suggest os as a better name.)

ostream& operator << (ostream& in, Pakage& A)
wht abt the functions of calcCost ??
they r nt returning the required values :(
did i inherited rightly ?
Use full words, please.

Pakage(unsigned a,unsigned b){weight=0;cost=0;}

What does this constructor do?
It sets weight and cost to zero. Which isn't what you want.
1
2
3
4
5
Pakage(unsigned a,unsigned b):
    weight(a),
    cost(b)
{
}


Now it uses initialization lists so that it sets them to a and b.

So this should work:
Over_night_pakage(unsigned a,unsigned b,unsigned c):Pakage(b,c){additional_fee=a=0;}

I would also suggest using better names for parameters, instead of a, b, and c, which nobody knows what they are for.
Last edited on
i am nt using these constructors
actually my problem is there in overriding functions
like
1
2
unsigned calcCost(){return weight*cost;}
double calcCost(){return flat_fee+(weight*cost);}

they r giving result in powers......
Topic archived. No new replies allowed.