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 3rd, 2012, 09:41 PM
Mansoor54 Mansoor54 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 7 Mansoor54 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 57 m 14 sec
Reputation Power: 0
Help this program

obj:- Funtion returning object and passing object as function
i create but some erorr

#include<iostream.h>
#include<conio.h>
class Distance{
int feets,inches;
public:
Distance()
{
feets=0;
inches=0;
}
Distance(int i)
{
feets=inches=i;
}
Distance(int f,int i)
{
feets=f;
inches=i;
}
Distance addDistance(Distance d1,Distance d2)
{
Distance d3;
d3.feets=d1.feets+d2.feets;
d3.inches=d1.inches+d2.inches;
return d3;
}
void show()
{
cout<<"\nFeets= "<<feets;
cout<<"\nInches= "<<inches;
}
};
void main()
{
Distance d2(10,20),d3;
int f,i;
clrscr();
cout<<"Enter Distance 1 feets: ";
cin>>f;
cout<<"Enter Distance 2 inches: ";
cin>>i;
Distance d1(f,i);
d3=addDistance(d1,d2);
d1.show();
d2.show();
d3.show();
getch();
}

Reply With Quote
  #2  
Old December 3rd, 2012, 11:53 PM
abhiakhi abhiakhi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 abhiakhi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 32 sec
Reputation Power: 0
Quote:
Originally Posted by Mansoor54
obj:- Funtion returning object and passing object as function
i create but some erorr

#include<iostream.h>
#include<conio.h>
class Distance{
int feets,inches;
public:
Distance()
{
feets=0;
inches=0;
}
Distance(int i)
{
feets=inches=i;
}
Distance(int f,int i)
{
feets=f;
inches=i;
}
Distance addDistance(Distance d1,Distance d2)
{
Distance d3;
d3.feets=d1.feets+d2.feets;
d3.inches=d1.inches+d2.inches;
return d3;
}
void show()
{
cout<<"\nFeets= "<<feets;
cout<<"\nInches= "<<inches;
}
};
void main()
{
Distance d2(10,20),d3;
int f,i;
clrscr();
cout<<"Enter Distance 1 feets: ";
cin>>f;
cout<<"Enter Distance 2 inches: ";
cin>>i;
Distance d1(f,i);
d3=addDistance(d1,d2);
d1.show();
d2.show();
d3.show();
getch();
}


d3=addDistance(d1,d2);
this line contains the error , u see, as addDistance() is a member function of class Distance hence it can be accessed only by variable/object (like d3) of that class using dot(.) operator and not indipendently. so the correct statement is
d3=d3.addDistance(d1,d2);

Reply With Quote
  #3  
Old December 4th, 2012, 09:26 PM
Mansoor54 Mansoor54 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 7 Mansoor54 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 57 m 14 sec
Reputation Power: 0
OK thanks friends

Reply With Quote
  #4  
Old December 5th, 2012, 10:44 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 43 m 11 sec
Reputation Power: 1800
Post

There is some semantically very suspect code in this:

Code:
Distance x(2) ;

Will instantiate a Distance object representing 2 feet and two inches. Is that really what you intended!?

Code:
Distance x = addDistance(Distance( 1, 6 ), Distance( 1, 7))

Will create a Distance object for 2 feet 13 inches, not 3 feet 1 inch.

Code:
Distance x(1,14)

Will create a Distance object for 1 feet 14 inches; it would make sense to normalise it to 2 feet 2 inches.

The whole thing can be simplified if you store all distances internally in inches (although this is the 21st Century millimetres might be better). So for example:

Code:
Distance( int i )
{
    inches = i ;
}

Distance( int f, int i )
{
    inches = f * 12 + i ;
}

void show()
{
    cout << "\nFeet = " << inches / 12 ;
    cout << "\nInches = " << inches % 12;
}

static Distance addDistance(const Distance& d1, const& Distance d2)
{
    return Distance( d1.inches + d2.inches ) ;
}


The member function addDistance() can be declared static to overcome your original problem. It is more normal to have a non-static member that just takes a single operand and adds it to the object for which it was called:

Code:
Distance& addDistance(const Distance& d )
{
    inches += d ;
    return *this ;
}


And this is an obvious opportunity to operator overloading:

Also "Feet" is the plural of "Foot", "Feets" is just not English.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Help this program

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