|
Write a C++ program that has class
1) math
Our math class has only one data member number and member function display that will display the data member number.
Write the constructor of our math class that will initialize the data member number with the value zero.
Our program will overload the following operators.
1. Plus +
2. Minus -
3. Multiplication *
After overloading these three operators our program will be able to add, subtract and multiply our math class object with the integer in main() after overloading these operators we should be able to write these statements in main().
math obj1, obj2;
obj1= obj2 + 10;
Above statement will call the member function operator + () and will add the 10 in the data member of obj2 and finally will return the math class object. Similarly we also overload the multiplication operator * and minus operator – so that our math class object will be able to multiply and subtract from integer values.
Also our plus + overloaded operator should be intelligent enough to accommodate the following statement in the main() function.
math obj1, obj2;
obj2= 10 + obj1;
for this we will have to write friend function that will be called automatically and will add the integer value 10 in the data member of obj1 and finally will return the math class object . Similarly write two more friend functions that will overload the multiplication and subtraction operator so that we will be able to write the following statement in the main() function.
math obj1, obj2;
obj2= 10 *obj1;
obj2= 10 - obj1;
Output should be similar to the following:
Sample output 1:
adding integer 10 in the object using statement: obj= obj + 10 ;
10
adding integer 10 in the object using statement: obj= 10 + obj;
20
Multiplying object with integer 20 using statement: obj= obj * 20 ;
400
Multiplying integer 20 with object using statement: obj= 20 * obj ;
8000
Subtracting 20 from object using statement: obj= obj - 20 ;
7980
Subtracting object from 10 using statement: obj= 10 - obj ;
-7970
The Code i wrote is below:
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
class math
{
private:
// Private data member of the calss math
int number ;
public:
// Constructor for math class
math() : number(0) {}
// friend functions
friend int operator+(int,math);
friend int operator-(int,math);
friend int operator*(int,math);
};
// Main function of the programm
void main ( )
{
math m1,m2 ; //create an object of class myClass1
int obj2;
cout << "adding integer 10 in the object using statement: obj=obj + 10 "<
|
|
|
|
|
Wow, I can see that the copy/paste functionality works fine on your computer .
Ok, but what's the question ? You want us to do your homework for you maybe ?
You will have more success if you post SPECIFIC questions, for the parts where you are stuck. Also, try to use the formatting tags when posting code !
|
|
|
|
|
i jst want u to chk my code that is giving sme errorz and i wrote the code in my qustion . Reply if u really wanna hlp.
|
|
|
|
|
rose aashii wrote:
i jst want u to chk my code that is giving sme errorz
What errors? Compiler, linker, run-time, assertion, exception, etc. Have you single-stepped through each line of code to see where you get unexpected results?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
You need to put the operator functions as member functions of the class (so first parameter of the function will be implicitly the this parameter). And you'll have to remove the * in the definitions of these function (return an int not a int* ).
You have luck that I'm in a good mood. Honnestly your post only deserve a 1. There is no specific question, you just want us to go through your code without giving us the errors, there is no formatting, you just made a copy paste of your homework and you don't even have the respect to use correct english. It is a forum here, not a mobile so avoid using SMS language
|
|
|
|
|
rose aashii wrote:
int *operator +(math m)
{
number = number + m.number;
return number;
}
ok, i'll not repeat what cedric already told you, but if you think a liitle on your code, you could see this :
you return an int* .
firstly, we use to return references instead of pointers when coding operators overloads.
secondly, do you really want here to modify the value of the operands ?
if you consider the operation : 2 + 2 , i should return 4, but not modify none. moreover, which part would you modify here ? 2 (non l-value) ?!
so, just return an int ...
and don't forget !!! reflection is the begining of a good code ! ;P
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
i won't give you the entiere solution (as homeworks are for grow up self knowledge), but i give you a start point by designing a good class (i changed the class name into CMathCalc to avoid redefining a supposed existing math identifier) :
MathCalc.h
<font color=blue>class</font> CMathCalc {
<font color=blue>double</font> m_Data;
<font color=blue>public</font>:
<font color=green>
CMathCalc();
CMathCalc(<font color=blue>double</font>);
<font color=blue>virtual</font> ~CMathCalc();
<font color=green>
<font color=blue>double</font> Display() <font color=blue>const</font>;
<font color=green>
<font color=blue>double operator</font> + (<font color=blue>double</font>) <font color=blue>const</font>;
<font color=blue>double operator</font> - (<font color=blue>double</font>) <font color=blue>const</font>;
<font color=blue>double operator</font> * (<font color=blue>double</font>) <font color=blue>const</font>;
CMathCalc& <font color=blue>operator</font> = (<font color=blue>double</font>);
CMathCalc& <font color=blue>operator</font> = (<font color=blue>const</font> CMathCalc&);
<font color=blue>double</font> <font color=blue>operator</font> + (<font color=blue>const</font> CMathCalc&) <font color=blue>const</font>;
<font color=blue>double</font> <font color=blue>operator</font> - (<font color=blue>const</font> CMathCalc&) <font color=blue>const</font>;
<font color=blue>double</font> <font color=blue>operator</font> * (<font color=blue>const</font> CMathCalc&) <font color=blue>const</font>;
<font color=blue>friend double operator</font> + (<font color=blue>double</font>, <font color=blue>const</font> CMathCalc&);
<font color=blue>friend double operator</font> - (<font color=blue>double</font>, <font color=blue>const</font> CMathCalc&);
<font color=blue>friend double operator</font> * (<font color=blue>double</font>, <font color=blue>const</font> CMathCalc&);
};
<font color=green>
<font color=blue>double operator</font> + (<font color=blue>double</font>, <font color=blue>const</font> CMathCalc&);
<font color=blue>double operator</font> - (<font color=blue>double</font>, <font color=blue>const</font> CMathCalc&);
<font color=blue>double operator</font> * (<font color=blue>double</font>, <font color=blue>const</font> CMathCalc&);
ok, i think you have enough information now to have a good mark...
cheers,
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Wow, you are so patient today
|
|
|
|
|
|
toxcct wrote:
bored with my project
Which Project
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
i'm currently making an application for a ski rent shop with oracle designer/forms...
quite indigest...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
The kind of things I hate to do...
|
|
|
|
|
cedric moonen wrote:
The kind of things I hate to do...
Me TOO, It's Obvious, VC++ programmer and DATABASE
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Hi this is pratap
i am working on datawarehousing tool implemented in VC
I am Implementing oledb connections in the tool.
For this i am using the classes CDataSource, CSession,CTables,CColumns,CPrimaryKeys, CForeignKeys etc.
every thing i am getting fine except the column properties like column length,precision, datatype name etc. can any one help in this.
expecting positive reply
thank you
pratap
|
|
|
|
|
Hi,
I have used the following codes to load a bitmap onto the dialog box for the background during onPaint.However I need to change the picture when i click another button. Is there a function which i can call to redraw the bitmap again? cos when i put these code into a function, it doesnt work.
Any idea?
[code]
CDC BmpDc;
CPaintDC dc(this);
VERIFY( BmpDc.CreateCompatibleDC(&dc) );
CBitmap BkBmp, *pOldBmp;
//BkBmp.
BkBmp.LoadBitmap(IDB_background);
pOldBmp = (CBitmap *)BmpDc.SelectObject(&BkBmp);
dc.BitBlt(0,0,m_nW,m_nH,&BmpDc,0,0,SRCCOPY);
BmpDc.SelectObject(pOldBmp);
[/code]
Newb VCer
|
|
|
|
|
If OnPaint() takes the bitmap ID from a member variable and paints it, your button handler might simply change the ID of the bitmap to draw and invalidate the window. Something like this:
<br />
m_BackBitmapID = IDB_Background1;<br />
<br />
Invalidate(); <br />
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
|
Are there any codes out there that helps me to estimate or calculate the center of a sphere from a set of data points on the surface of the sphere? for eg., least-squares fitting of a set of points to the equation of a sphere?
THANKS!
|
|
|
|
|
mm don't know any code but if I'm not mistaken the radius is in the equation correct?
this means you have the distance of each point on the sphere to the center.
take to points and calculate which point is on distance = radius to the two known points.
If you don't know your formulas, search the net. you need the equation of the sphere and the equations for calculating the distances. (Don't know it by heart sorry, it 's something with roots and power to two etc)
good luck.
No hurries, no worries.
|
|
|
|
|
At first I tried to remember 3D math, surface equations and surface normals
But solution is easy if I do not misunderstand
You have limited sample points (x,y,z) of a sphere.
Search all samples and obtain Minimim and Maximum value of X,Y,Z;
The center of the sphere is (Xcenter,Ycenter,Zcenter)
The equation should be so;
Xcenter = (Xmin + Xmax) / 2
Ycenter = (Ymin + Ymax) / 2
Zcenter = (Zmin + Zmax) / 2
I hope it helps you
|
|
|
|
|
Or you can apply clasiccal method;
You should have at least three surface point to obtain a sphere.
(Xo,Yo,Zo) is center coordiane of sphere and r is half of diameter
Sphere equation is
(X - Xo)^2 + (Y - Yo)^2 + (Z - Zo)^2 = r^2
We have 3-samples
Sample1 : (X1,Y1,Z1)
Sample2 : (X2,Y2,Z2)
Sample3 : (X3,Y3,Z3)
Place these three sample to the above equation, and then you will obtain 3 equation with unknown Xo,Yo,Zo parameters. You can easily calculate these with three equation.
|
|
|
|
|
I just wrote a toolband code to create a toolbar...when i compiled that, the new browser created has the title corrupted..Do any body know abt this..
Replies will be appreciated...(Eg: if the title of the page is "about blank" only "a" comes there....)
Thanx in Advance..
|
|
|
|
|
SunSelvaS wrote:
(Eg: if the title of the page is "about blank" only "a" comes there....)
Just a wild guess but...
Whenever I see only the first char of a string being displayed, it always make me think of a UNICODE/ANSI mismatch problem. Any chance you are passing a unicode string (WCHAR) to something that takes it as ANSI (char)?
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
Try to modifying the resource string for this title id and check.
if u r using some toolkit for GUI( Xp style) then its worth checking the resource table.
Lokiz
|
|
|
|
|
alok... i deleted all the .ncb(not .ncw) file.. and now the code assistant is working..great man.. thanks a lot.. but still the "Symbol not defined" is displayed when u right click on a variable to see where it is declared... can u help me out ?
vivek
|
|
|
|