View Full Version : another c++ coding question...
say i have a class Geometry that contains a char *Name member and there are no functions to set Name
and i have a class Box
i create an array of Geometry with one of the array objects being Box
when defined i include the Name
ex:
Geometry *array[ITEM_COUNT];
array[0] = new Box = ("Box 1", 3, 4, 5) // numbers irrelevant
when it goes to Box.h and hits the constructor should i create a Geometry object with "Box 1" as one of the parameters or should i make char *Name protected in Geometry.h and strcpy that shit (i dont even know if i can do that?)?
do you have ocd? no? then stick to plain C.
Devious
02-28-2008, 04:52 PM
lol
PM MX, he knows everything
Devious
02-28-2008, 04:54 PM
i wish i knew everything
Vershun
02-28-2008, 04:54 PM
say i have a class Geometry that contains a char *Name member and there are no functions to set Name
and i have a class Box
i create an array of Geometry with one of the array objects being Box
when defined i include the Name
ex:
Geometry *array[ITEM_COUNT];
array[0] = new Box = ("Box 1", 3, 4, 5) // numbers irrelevant
when it goes to Box.h and hits the constructor should i create a Geometry object with "Box 1" as one of the parameters or should i make char *Name protected in Geometry.h and strcpy that shit (i dont even know if i can do that?)?
I don't really understand your question. Geometry can't hold Box objects unless Box is inherited from the Geometry class or vice-versa, in which you'll get into all sorts of dynamic casting issues (virtual function stuff probably).
Also if you're using C++ use the string class, not char*.
say i have a class Geometry that contains a char *Name member and there are no functions to set Name
and i have a class Box
i create an array of Geometry with one of the array objects being Box
when defined i include the Name
ex:
Geometry *array[ITEM_COUNT];
array[0] = new Box = ("Box 1", 3, 4, 5) // numbers irrelevant
when it goes to Box.h and hits the constructor should i create a Geometry object with "Box 1" as one of the parameters or should i make char *Name protected in Geometry.h and strcpy that shit (i dont even know if i can do that?)?
no anon you would do Box::Box(...):Geometry(...)
but im stuck again
I don't really understand your question. Geometry can't hold Box objects unless Box is inherited from the Geometry class or vice-versa, in which you'll get into all sorts of dynamic casting issues (virtual function stuff probably).
Also if you're using C++ use the string class, not char*.
trust me i would use string if it was my choice. it is inherited btw i forgot to mention but im pretty sure i woul do it the way i stated. in Geometry(...) would i put const char *x and it would pass?
i have some virtual functions for other stuff.
villain
02-28-2008, 05:56 PM
reword your question i can help
// Box.h
#ifndef BOX_H
#define BOX_H
#include <iostream>
using namespace std;
class Box : public Geometry
{
private:
double length;
double width;
double height;
const char *type;
const char name[3] = 'box';
public:
Box()
{
length = 0.0;
width = 0.0;
height = 0.0;
}
Box::Box(double l, double w, double h):Geometry(*name, *type)
{
length = l;
height = h;
width = w;
}
};
#endif
// Geometry.h
#ifndef GEOMETRY_H
#define GEOMETRY_H
#include <iostream>
#include <cstring>
using namespace std;
const int STANDARD = 51;
class Geometry
{
protected:
char *Name;
char *Type;
public:
Geometry()
{
char *Name = new char[STANDARD] = '\0';
char *Type = new char[STANDARD] = '\0';
}
Geometry(const char *n, const char *t)
{
strncpy(Name, n, STANDARD);
Name[STANDARD -1] = '\0';
strncpy(Type, t, STANDARD);
Type[STANDARD -1] = '\0';
}
/*const char *getName() const
{ return *Name; }
const char *getType() const
{ return *Type; } */
/*virtual double computeSurface() const;
virtual double computeArea() const;*/
};
#endif
// GeoMAIN.cpp
#include <iostream>
#include "Geometry.h"
#include "Box.h"
#include "Cylinder.h"
#include "Sphere.h"
using namespace std;
void main()
{
Box("Box 1", 3, 4, 3);
}
okay so im just testing it to see if it works and it is not working right now.
problem is with this:
Box::Box(double l, double w, double h):Geometry(*name, *type)
i dont know what to do though, it doesn't go over this at all in the book and my prof went over it really briefly.
actually it might just be that i can completely fucked up box.h but i still dont know what to do
EDIT:
#ifndef BOX_H
#define BOX_H
#include <iostream>
using namespace std;
class Box : public Geometry
{
private:
double length;
double width;
double height;
char *type;
const char *name;
public:
Box::Box(const char *name, double l, double w, double h):Geometry(name)
{
length = l;
height = h;
width = w;
}
};
#endif
I still don't know what to do with Geometry(name)
its giving me an error saying
Error 3 error C2664: 'Geometry::Geometry(const Geometry &)' : cannot convert parameter 1 from 'const char **' to 'const Geometry &' c:\users\owner\desktop\matt - geometry\matt - geometry\box.h 17 matt - geometry
AIM anyone? pls this is starting to piss me off BAAAAAAD
i keep changing things that takes away one error but brings another
rpliving
02-28-2008, 07:40 PM
your teacher doesnt let you use strings? Ya i had the same bitch.
his argument is that it is harder for students to use c-strings and that c-strings aren't "natural" to C
ChewY
02-28-2008, 09:13 PM
his argument is that it is harder for students to use c-strings and that c-strings aren't "natural" to C
sounds like a hippie
AIM anyone? pls this is starting to piss me off BAAAAAAD
If you still need help, you can pm me and I'll help you out.
or you can drop the source for the Geometry class here
If you still need help, you can pm me and I'll help you out.
or you can drop the source for the Geometry class here
thanks for the offer but i FINALLY figured it out (hooray!)
his argument is that it is harder for students to use c-strings and that c-strings aren't "natural" to C
I think that using char* is better in school, but only because it breeds better hackers. String mismanagement and pointer fuck-ups totally teach you everything you need to know to be able to smash a stack or understand buffer overflows.
wow i just realized how much i fucked up that statement
i meant to say strings arent natural to c but im sure you guys knew what i meant
wow i just realized how much i fucked up that statement
i meant to say strings arent natural to c but im sure you guys knew what i meant
If your prof actually said that, he's a fucking idiot considering there are functions in x86 asm (MOVS, etc) specifically created for string manipulation.
he meant that it was written after C and so its not part of the central language, but then again a LOT of things that are integral to coding in C were not included in the original write. I think he just wants us to learn how to use them well because strings are really easy to use and if we can do it the hard way then doing it the easy way is a piece of cake.
I said above that I think it should be taught as char*, I was just saying that the reason that it's not "natural" is ridiculous.
oh lol yeh hes kinda insane
plus while fucking around with my code i realized the usefulness of virtual functions. its pretty cool.
Vershun
02-29-2008, 01:01 PM
My bad sorry I didn't help much I didn't realize box inherited geometry.
Virtual functions are pretty cool I guess. There's a lot in C++ that I don't use very much if at all: virtual functions, multiple inheritance, and operator overloading to name a few. Templating is probably the best thing to come out of C++.
And he was probably saying that strings aren't natural to C in that there's nothing in the C language that explicitly defines strings; you're just dealing with a byte array and it doesn't care whether it's a valid string or not as long as \0 comes at the end.
My bad sorry I didn't help much I didn't realize box inherited geometry.
lol its cool, forced me to learn for myself and although it took me a while to figure it out its probably better because now i know more about how it works.
Virtual functions are pretty cool I guess. There's a lot in C++ that I don't use very much if at all: virtual functions, multiple inheritance, and operator overloading to name a few. Templating is probably the best thing to come out of C++.
Haha, ya thats all the stuff thats in this chapter. :P
And he was probably saying that strings aren't natural to C in that there's nothing in the C language that explicitly defines strings; you're just dealing with a byte array and it doesn't care whether it's a valid string or not as long as \0 comes at the end.
also templates don't recognize them. oh shi-
same program...
#include <iostream>
#include <cmath>
#include "Geometry.h"
#include "Box.h"
#include "Cylinder.h"
#include "Sphere.h"
using namespace std;
const double Pi = 3.14;
void Report(Geometry *x);
void main()
{
Geometry *geo[5];
geo[0] = new Box("Box 1", 1, 2, 3);
geo[1] = new Sphere("Sphere 1", 3);
geo[2] = new Cylinder("Cylinder 1", 4, 2);
geo[3] = new Sphere("Sphere 2", 4);
Report(*geo);
}
void Report(Geometry *geo)
{
for(int x = 0; x < 4; x++)
{
cout << "Surface area: ";
geo[x]->computeSurface();
cout << "Volume: ";
geo[x]->computeVolume();
}
}
double Box::computeSurface() const
{
double surface = 0;
surface = ((2*(height + width)) + (2*(height + length)) + (2*(width + length)));
return surface;
}
double Box::computeVolume() const
{
double volume = 0;
volume = (height * width * length);
return volume;
}
double Sphere::computeSurface() const
{
double surface = 0;
surface = ((4*Pi) * pow(radius, 2));
return surface;
}
double Sphere::computeVolume() const
{
double volume = 0;
volume = (((4/3)*Pi) * pow(radius, 3));
return volume;
}
double Cylinder::computeSurface() const
{
double surface = 0;
surface = (2*(Pi * pow(radius, 2)) + (2*(Pi * radius) * height));
return surface;
}
double Cylinder::computeVolume() const
{
double volume = 0;
volume = ((Pi * pow(radius, 2)) * height);
return volume;
}
do i have to overload the [] function to get this to work? if so that fucking blows.
Vershun
03-01-2008, 05:49 PM
same program...
#include <iostream>
#include <cmath>
#include "Geometry.h"
#include "Box.h"
#include "Cylinder.h"
#include "Sphere.h"
using namespace std;
const double Pi = 3.14;
void Report(Geometry *x);
void main()
{
Geometry *geo[5];
geo[0] = new Box("Box 1", 1, 2, 3);
geo[1] = new Sphere("Sphere 1", 3);
geo[2] = new Cylinder("Cylinder 1", 4, 2);
geo[3] = new Sphere("Sphere 2", 4);
Report(*geo);
}
void Report(Geometry *geo)
{
for(int x = 0; x < 4; x++)
{
cout << "Surface area: ";
geo[x]->computeSurface();
cout << "Volume: ";
geo[x]->computeVolume();
}
}
double Box::computeSurface() const
{
double surface = 0;
surface = ((2*(height + width)) + (2*(height + length)) + (2*(width + length)));
return surface;
}
double Box::computeVolume() const
{
double volume = 0;
volume = (height * width * length);
return volume;
}
double Sphere::computeSurface() const
{
double surface = 0;
surface = ((4*Pi) * pow(radius, 2));
return surface;
}
double Sphere::computeVolume() const
{
double volume = 0;
volume = (((4/3)*Pi) * pow(radius, 3));
return volume;
}
double Cylinder::computeSurface() const
{
double surface = 0;
surface = (2*(Pi * pow(radius, 2)) + (2*(Pi * radius) * height));
return surface;
}
double Cylinder::computeVolume() const
{
double volume = 0;
volume = ((Pi * pow(radius, 2)) * height);
return volume;
}
do i have to overload the [] function to get this to work? if so that fucking blows.
lol naw you don't. I don't know what problem you're having but I'm sure it stems from:
Geometry *geo[5];
Should just be Geometry geo[5]; from looking at the way you're using it (right now you're making a pointer to an array instead of just an array).
EDIT: sorry i meant the report function
EDIT AGAIN!: lol im an idiot, i dont know why i was trying to use ->
Vershun
03-01-2008, 06:00 PM
EDIT AGAIN!: lol im an idiot, i dont know why i was trying to use ->
Aye well you're throwing around an extra pointer you don't need which is pretty confusing (probably why you might've thought you needed the -> operator).
For your sanity's sake you might want to get rid of the pointer so it looks like this:
void main()
{
Geometry geo[5];
geo[0] = new Box("Box 1", 1, 2, 3);
geo[1] = new Sphere("Sphere 1", 3);
geo[2] = new Cylinder("Cylinder 1", 4, 2);
geo[3] = new Sphere("Sphere 2", 4);
Report(geo);
}
void Report(Geometry *geo)
{
david blaine
03-01-2008, 06:12 PM
PM MX, he knows everything
i don't know much about C++ classes. i mainly use C, classes aren't really relevant, i believe, when coding in C#/C++. If I need to make a program with several different drivers, I use java. ask Vershun.
i'm only good with Java class abstraction and encapsulation. but looking at your code, it seems like you're using the wrong type class. you could translate this code to java and use a LinkedList to solve any pointer problems you're having....
ToastY
03-01-2008, 06:32 PM
LOL
Error 3 error C2259: 'Geometry' : cannot instantiate abstract class c:\users\owner\desktop\matt - geometry\matt - geometry\geomain.cpp 14 matt - geometry
O_O thats what happens when i tried it your way versh. i have no idea what it means though.
but now i have ANOTHER problem. all the compute functions work alright for box, but when it gets to sphere they dont work for some reason... like it won't even go into the function theres a problem when it tries.
http://la.gg/upl/matt_macdonald_-_geometry.rar
Vershun
03-01-2008, 08:01 PM
We talked in IMs but the solution was to drop the pointer in the function call and make the parameter accept **.
Report(geo);
return 0;
}
void Report(Geometry **geo)
and the pointers in the report func had to be switched to geo[x]->computeX();
kicken
10-18-2008, 05:56 PM
K well i got the same program and im having the same problem with the whole
rror C2664: 'Geometry::Geometry(const Geometry &)' : cannot convert parameter 1 from 'const char *' to 'const Geometry &'
1> Reason: cannot convert from 'const char *' to 'const Geometry'
Just wondering what you did to solve that.
Thanks.
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.