I'm really new to C++ and just getting started with classes (it would be helpful if someone could post a simple a class example below), and this was my first attempt of using them. With school starting up again, I figured that I'd make it something that could be useful. In this case a program that calculates the area of basic shapes (once I get classes working, I'll add more). So here it is.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
const float pi = 3.14;
class ShapeMath
{
protected:float x, y, r;
public:
void RectValues (float,float,string);
float RectReturn ();
void CircleValues (float,float,string);
float CircleReturn ();
};
void ShapeMath::RectValues (float a, float b, string input)
{
cout << "Width: ";
cin >> input;
stringstream(input) >> a;
cout << "Height: ";
cin >> input;
stringstream(input) >> b;
cout << "\n";
x = a;
y = b;
return;
}
float ShapeMath::RectReturn ()
{
return (x * y);
}
void ShapeMath::CircleValues (float a, float b, string input)
{
cout << "Radius: ";
cin >> input;
stringstream(input) >> a;
cout << "\n";
r = a;
return;
}
float ShapeMath::CircleReturn ()
{
return (r * r * pi);
}
int main ()
{
string input; // Variable used to get user input
float answer; // Variable used to print results
bool loop = true; // Variable used to continue the loop
ShapeMath Rect, Circle; // Objects
while (loop == true)
{
cout << "1) Rectangle\n2) Circle\n3) Quit\n\n";
cin >> input;
if (input == "1")
{
Rect.RectValues;
cout << answer;
}
else if (input == "2")
{
Circle.CircleValues;
cout << answer;
}
else if (input == "3")
{
loop = false;
break;
}
cout << "\n";
}
}Help. please? For users of VC++ and other enviroments that don't have a decent debugger (as far as I'm aware), my error message says something about overloading. I didn't really understand that chapter of cplusplus.com/doc/tutorial/.
Edit:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
const float pi = 3.14;
class ShapeMath
{
protected:float x, y, r;
public:
void RectValues (float,float,string);
float RectReturn ();
void CircleValues (float,float,string);
float CircleReturn ();
void TrigValues (float,float,string);
float TrigReturn ();
};
void ShapeMath::RectValues (float a, float b, string input)
{
cout << "Width: ";
cin >> input;
stringstream(input) >> a;
cout << "Height: ";
cin >> input;
stringstream(input) >> b;
cout << "\n";
x = a;
y = b;
return;
}
float ShapeMath::RectReturn ()
{
return (x * y);
}
void ShapeMath::CircleValues (float a, float b, string input)
{
cout << "Radius: ";
cin >> input;
stringstream(input) >> a;
cout << "\n";
r = a;
return;
}
float ShapeMath::CircleReturn ()
{
return (r * r * pi);
}
void ShapeMath::TrigValues (float a, float b, string input)
{
cout << "Width: ";
cin >> input;
stringstream(input) >> a;
cout << "Height: ";
cin >> input;
stringstream(input) >> b;
cout << "\n";
x = a;
y = b;
}
float ShapeMath::TrigReturn ()
{
return (x * y * 0.5);
}
int main ()
{
string input; // Variable used to get user input
float answer; // Variable used to print results
bool loop = true; // Variable used to continue the loop
ShapeMath Rect, Circle, Trig; // Objects of ShapeMath class
while (loop == true)
{
cout << "1) Rectangle\n2) Circle\n3) Triangle\n4) Quit\n\n";
cin >> input;
if (input == "1")
{
Rect.RectValues (0, 0, "");
answer = Rect.RectReturn ();
cout << answer;
}
else if (input == "2")
{
Circle.CircleValues (0, 0, "");
answer = Circle.CircleReturn ();
cout << answer;
}
else if (input == "3")
{
Trig.TrigValues (0, 0, "");
answer = Trig.TrigReturn ();
cout << answer;
}
else if (input == "4")
{
loop = false;
break;
}
cout << "\n";
}
}Perfect. Added triangles, too.
Last edited by maxskywalker (2011-09-07 16:34:21)
Offline
I have (I think) made your code work in c. The following calculates the area of a rectangle and a circle. I used a lot of things in the program to show you some features of c.
Ok code
[file name: mainpart.c]
#include <stdio.h>
#include "mathss.h"
int enter(int inFunction)
{
if (inFunction == 1)
{
double xx, yy;
float x,y,result;
printf("Enter x value:");
scanf("%f", &x);
printf("Enter y value:");
scanf("%f", &y);
xx = (double)x;
yy = (double)y;
result = (float)rectangleArea(xx,yy);
printf("Result: %f", result);
}else {
if (inFunction == 2)
{
double rr;
float r, result;
printf("Enter radius:");
scanf("%f", &r);
rr = (double)r;
result = (float)circleArea(rr);
printf("Result: %f", result);
}else{
printf("Not A valid operation");
};
};
return 0;
}
int main(void)
{
int functionTwoPerform;
printf("This program can calulate the area or a rectangle and a circle \n");
printf("Enter ""1"" to calc area of rectangle \n");
printf("Enter ""2"" to calc area of circle \n");
scanf("%d", &functionTwoPerform);
enter(functionTwoPerform);
return 0;
}[file name: mathss.h]
double circleArea(double inRadius); double rectangleArea(double inX, double inY);
[file name: mathss.c]
#include <stdio.h>
#define PI 3.141519
double circleArea(double inRadius)
{
double theCircleArea;
theCircleArea = (inRadius * inRadius * PI);
return theCircleArea;
}
double rectangleArea(double inX, double inY)
{
double theRectangleArea;
theRectangleArea = inX * inY;
return theRectangleArea;
}Compile with gcc using the following
gcc -Wall -pedantic -ansi -o mainpart mainpart.c mathss.c
Classes are more a java and C#, VB.Net thing.
And I know that this is not c++ but this will work on many OS's going way back as it's C89.
My site Offline
what-the wrote:
Classes are more a java and C#, VB.Net thing.
sir ..
C++ was originally known as C with Classes and it has decent classs support
*_* maybe just me though.. yeah i might be code blind lol
Offline
what-the wrote:
I have (I think) made your code work in c. The following calculates the area of a rectangle and a circle. I used a lot of things in the program to show you some features of c.
Ok code
[file name: mainpart.c]Code:
#include <stdio.h> #include "mathss.h" int enter(int inFunction) { if (inFunction == 1) { double xx, yy; float x,y,result; printf("Enter x value:"); scanf("%f", &x); printf("Enter y value:"); scanf("%f", &y); xx = (double)x; yy = (double)y; result = (float)rectangleArea(xx,yy); printf("Result: %f", result); }else { if (inFunction == 2) { double rr; float r, result; printf("Enter radius:"); scanf("%f", &r); rr = (double)r; result = (float)circleArea(rr); printf("Result: %f", result); }else{ printf("Not A valid operation"); }; }; return 0; } int main(void) { int functionTwoPerform; printf("This program can calulate the area or a rectangle and a circle \n"); printf("Enter ""1"" to calc area of rectangle \n"); printf("Enter ""2"" to calc area of circle \n"); scanf("%d", &functionTwoPerform); enter(functionTwoPerform); return 0; }[file name: mathss.h]
Code:
double circleArea(double inRadius); double rectangleArea(double inX, double inY);[file name: mathss.c]
Code:
#include <stdio.h> #define PI 3.141519 double circleArea(double inRadius) { double theCircleArea; theCircleArea = (inRadius * inRadius * PI); return theCircleArea; } double rectangleArea(double inX, double inY) { double theRectangleArea; theRectangleArea = inX * inY; return theRectangleArea; }Compile with gcc using the following
gcc -Wall -pedantic -ansi -o mainpart mainpart.c mathss.c
Classes are more a java and C#, VB.Net thing.
And I know that this is not c++ but this will work on many OS's going way back as it's C89.
Um, okay.... I really only feel that I need compatability for Mac OS X and Windows NT, so I'd rather stick with C++. I have other reasons, too (like newdawnengine.weebly.com).
I have NO idea what GCC is, and I'd rather not work with multiple files until later.
Maybe if you could tell me what was wrong with my code, I could fix it.
Last edited by maxskywalker (2011-09-06 14:08:48)
Offline
gcc is a compiler for c c++ Objective-C and much more. (works on Linux and Windows).
http://gcc.gnu.org/
Oh and by the way your not passing your parameters into the functions.
void RectValues (float,float,string);
void CircleValues (float,float,string);
Circle.CircleValues; Is what you have written, where are you paremeters the floats and the string?
Circle.CircleValues( parameters go here);
You seem to have done it in the most confusing way possible.
My site Offline
what-the wrote:
gcc is a compiler for c c++ Objective-C and much more. (works on Linux and Windows).
http://gcc.gnu.org/
Oh and by the way your not passing your parameters into the functions.
void RectValues (float,float,string);
void CircleValues (float,float,string);
Circle.CircleValues; Is what you have written, where are you paremeters the floats and the string?
Circle.CircleValues( parameters go here);
You seem to have done it in the most confusing way possible.
Oh. Thanks!
Offline
what-the wrote:
gcc is a compiler for c c++ Objective-C and much more. (works on Linux and Windows).
http://gcc.gnu.org/
Oh and by the way your not passing your parameters into the functions.
void RectValues (float,float,string);
void CircleValues (float,float,string);
Circle.CircleValues; Is what you have written, where are you paremeters the floats and the string?
Circle.CircleValues( parameters go here);
You seem to have done it in the most confusing way possible.
Thanks! This fixed it. Like I said, I'm still really new to C++. Also, the main reason that this is confusing and illogical is that it was meant as practice to test most of my knowledge of C++.
Offline