greetings, fellow internet!

welcome to digitalgangster.com, the coolest community for people who hack the gibson and make bajillions of dollars off of online marketing. please click here to register an account (it's free) and join in on a plethora of discussions with the internet elite.

ASGPHE,
ytcracker, original digital gangster
follow me on twitter
fan me on facebook
Results 1 to 39 of 39

Thread: another c++ coding question...

  1. #1

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default 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?)?


  2. #2
    . ryph's Avatar
    Join Date
    Apr 2005
    Location
    NJ
    Posts
    12,404

    Default

    do you have ocd? no? then stick to plain C.
    "Americans like to believe in American exceptionalism, that the United States is a force for good around the world, not just another country pursuing its interests via geopolitical horse-trading. This is part of why there is such a visceral public backlash against WikiLeaks -- because it lays bare U.S. diplomacy in all its blunt, unromantic reality."

  3. #3

    Default

    lol
    " me to bookoo.. oh me sow howney "

  4. #4
    admin KOiN's Avatar
    Join Date
    Jul 2005
    Posts
    29,750

    Default

    PM MX, he knows everything
    PM me with forum questions or concerns and I will do my best to help

  5. #5

    Default

    i wish i knew everything
    " me to bookoo.. oh me sow howney "

  6. #6
    I am Fredryck Fox! Vershun's Avatar
    Join Date
    May 2005
    Location
    Denver, CO
    Posts
    16,123

    Default

    Quote Originally Posted by Anon View Post
    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*.

  7. #7

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    Quote Originally Posted by Anon View Post
    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


  8. #8

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    Quote Originally Posted by Vershun View Post
    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.


  9. #9

    Default

    reword your question i can help

  10. #10

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    Code:
     // 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
    Code:
    // 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
    Code:
    // 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.


  11. #11

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    actually it might just be that i can completely fucked up box.h but i still dont know what to do


  12. #12

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    EDIT:

    Code:
    #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


  13. #13

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    AIM anyone? pls this is starting to piss me off BAAAAAAD

    i keep changing things that takes away one error but brings another


  14. #14

    Default

    your teacher doesnt let you use strings? Ya i had the same bitch.

  15. #15

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    his argument is that it is harder for students to use c-strings and that c-strings aren't "natural" to C


  16. #16
    EVERYTHING I POST IS TOTAL GARBAGE AND QBERT AND RMK AND NOFX HATE ME AND KALYES AN IDIET ChewY's Avatar
    Join Date
    Oct 2006
    Posts
    11,435

    Default

    Quote Originally Posted by Anon View Post
    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
    Circumcised by faith.

  17. #17

    Default

    Quote Originally Posted by Anon View Post
    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

  18. #18

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    Quote Originally Posted by awol View Post
    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!)


  19. #19

    Default

    Quote Originally Posted by Anon View Post
    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.

  20. #20

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    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


  21. #21

    Default

    Quote Originally Posted by Anon View Post
    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.

  22. #22

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    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.


  23. #23

    Default

    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.

  24. #24

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    oh lol yeh hes kinda insane


  25. #25

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    plus while fucking around with my code i realized the usefulness of virtual functions. its pretty cool.


  26. #26
    I am Fredryck Fox! Vershun's Avatar
    Join Date
    May 2005
    Location
    Denver, CO
    Posts
    16,123

    Default

    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.

  27. #27

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    Quote Originally Posted by Vershun View Post
    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.

    Quote Originally Posted by Vershun View Post
    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

    Quote Originally Posted by Vershun View Post
    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-


  28. #28

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    same program...

    Code:
    #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.


  29. #29
    I am Fredryck Fox! Vershun's Avatar
    Join Date
    May 2005
    Location
    Denver, CO
    Posts
    16,123

    Default

    Quote Originally Posted by Anon View Post
    same program...

    Code:
    #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).

  30. #30

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    EDIT: sorry i meant the report function


  31. #31

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    EDIT AGAIN!: lol im an idiot, i dont know why i was trying to use ->


  32. #32
    I am Fredryck Fox! Vershun's Avatar
    Join Date
    May 2005
    Location
    Denver, CO
    Posts
    16,123

    Default

    Quote Originally Posted by Anon View Post
    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)
    {

  33. #33
    My only fear is the unknown. david blaine's Avatar
    Join Date
    May 2005
    Posts
    8,370

    Default

    Quote Originally Posted by koin View Post
    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....
    Last edited by david blaine; 03-01-2008 at 06:15 PM.

  34. #34
    I Love The Taste Of Jamels Cock.........
    Join Date
    Jul 2007
    Location
    MiYAYO
    Posts
    2,102

    Default

    LOL

  35. #35

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    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.


  36. #36

  37. #37
    I am Fredryck Fox! Vershun's Avatar
    Join Date
    May 2005
    Location
    Denver, CO
    Posts
    16,123

    Default

    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)

  38. #38

    Join Date
    Jul 2007
    Location
    Internets
    Posts
    3,169

    Default

    and the pointers in the report func had to be switched to geo[x]->computeX();


  39. #39

    Default Same Program

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •