Thread: Advice

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    4

    Advice


    Im learning C++ and have just started using Objects so i made a pgm to get the the volume from different size boxes. the pgm runs good but i want to now if im using the proper way to code Objects.
    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    class Box
    {
    public:
    	double length;
    	double breadth;
    	double height;
    	double base;
    	double depth;
    	double radius;
    		
    	//Memebr function decleration
    	double getVolume(void);
    	double getVolumeTri(void);
    	double getVolumeCyl(void);
    	double getVolumeCone(void);
    	
    	void setLength( double len);
    	void setBreadth( double bre);
    	void setHeight( double hei);
    	void setBase (double bas);
    	void setDepth (double dep);
    	void setRadius(double rad);
    	
    };
    
    //functions begin
    double Box::getVolume(void)//volume for square box 1 and 2
    {
    	return length * breadth * height;
    }
    double Box::getVolumeTri(void)//volume for triangle box
    {
    	return 0.5 * base * height * depth; 
    }
    double Box::getVolumeCyl(void)//volume of cyllinder
    {	
    	return M_PI * radius * radius * height;
    }
    double Box::getVolumeCone(void)//volume for cone
    {
    	return 0.33333 * M_PI * M_PI * height;
    }
    
    
    void Box::setLength (double len)
    {
    	length = len;
    }
    void Box::setBreadth (double bre)
    {
    	breadth = bre;
    }
    void Box::setHeight (double hei)
    {
    	height = hei;
    }
    void Box::setBase (double bas)
    {
    	base = bas;
    }
    void Box::setDepth (double dep)
    {
    	depth = dep;
    }
    void Box::setRadius (double rad)
    {
    	radius = rad;
    }
    	
    
    int main()
    {
    	
    
    	Box Box1;
    	Box Box2;
    	Box Box3;
    	Box Box4;
    
    
    double volume = 0.0;
    double volumeCone = 0.0;
    double volumeTri = 0.0;
    double volumeCyl = 0.0;
    	
    int choice;
    
    cout << "for box choose 1: "<< endl << endl << "for cone box choose 2: "<< endl << endl << 
    	"for triangle box choose 3: "<< endl << endl << "for Cyllinder choose 4: "<< endl << endl;
    
    cin >> choice;
    	
    switch (choice) {
    	case 1:
    	
    	//box 1 specs
    	cout << "What is your height for the box in inches: " << endl;
    	cin >> Box1.height;
    	cout << "What is your lenth for the box in inches: " << endl;
    	cin >> Box1.length;
    	cout << "What is your width for the box in inches:  "  << endl;
    	cin >> Box1.breadth;
    		//volume of box 1
    		volume = Box1.getVolume();
    		cout << "Volume of Box = " << volume <<" "<< "cubic inches" << endl << endl;
    		break;
    	
    	case 2:
    	//box 2 specs
    	cout << "What is your height for the cone in inches: " << endl;
    	cin >> Box2.height;
    	cout << "What is your radius for the cone in inches: "  << endl;
    	cin >> Box2.radius;
    		volumeCone = Box2.getVolumeCone();
    		cout << "Volume of Cone = " << volumeCone <<" "<< "cubic inches" << endl << endl;		
    		break;
    		
    	case 3:
    	//triangle box
    	cout << "What is your height for the triangle box in inches: " << endl;
    	cin >> Box3.height;
    	cout << "What is your base for the triangle box in inches: "  << endl;
    	cin >> Box3.base;
    	cout << "What is your depth for the box in inches: " << endl;
    	cin >> Box3.depth;
    		//volume of a triangle
    		volumeTri = Box3.getVolumeTri();
    		cout << "Volume of Triangle Box = " << volumeTri <<" "<< "cubic inches" << endl << endl;		
    		break;
    		
    	case 4:	
    	//cyllinder
    	cout << "What is your height of the cylllinder in inches: " << endl;
    	cin >> Box4.height;
    	cout << "What is the radius for the cyllinder in inches: "  << endl;
    	cin >> Box4.radius;
    		//volume of a cyllinder
    		volumeCyl = Box4.getVolumeCyl();
    		cout << "Volume of Cyllinder = " << volumeCyl <<" "<< "cubic inches" << endl << endl;
    		break;
    	
    	}	
    	
    	
    	return 0;
    }
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    From an object-oriented design standpoint, this is a poor class design; you can define a "Box", but later interpret it as either a cuboid, cylinder, cone or triangular prism while in reality it can only be one thing or the other. The object should "know" what it is. This is a problem for example because you can have a cuboid and then set its radius for example, which makes no sense at all. In fact its worse, because radius can be set but has no involvement in a cuboid - you can ask for the volume and the code volume for the same object when it may be neither.

    You might define a base class "Box" and then inherit from that separate classes for the box shapes you need. The base class would have a single virtual function "volume" (or getVolume is you prefer, but the "get" is unnecessary IMO - the signature makes it implicit that it is a "getter"), the volume function may then be implemented for each explicit box shape in the appropriate subclasses.

    You might also implement a constructor that takes parameters to define the box on instantiation, and a default constructor to define perhaps a "zero sized" box - as it stands you can have an initialised or partially initialised box, and get some nonsense results.

    Note also that your formula for cone volume is incorrect, and the multiplication by 0.33333 ill-advised since it unnecessarily loses precision by the arbitrary choice of five significant figures - better( and correct):

    Code:
    return (M_PI * radius * radius * height) / 3.0 ;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    4
    Thank you for your input my goal is to improve upon what i do in c++ im a computer engineer major and C++ is one of my classes. This is not homework it is intended for me to learn the language inside out. I only took the 1 st class and in jan i start the second part but everything you explained i have covered in class at minimum level and i will rewrite it as you stated and try to implement your suggestion and post back my results. Even though i am a engineer major i like programing languages and have learned many different ones that are not part of my major. Thank you for your time and i will be posting back with results hoping for more guidance from people like you
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    4
    Is this what you meant ?
    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    class Volume1
    {
    public:
    	double length;
    	double breadth;
    	double height;
    	double base;
    	double depth;
    	double radius;
    		
    	//Memebr function decleration
    	double VolumeBox(void);
    	double VolumeTri(void);
    	double VolumeCyl(void);
    	double VolumeCone(void);
    	
    	void Length( double len);
    	void Breadth( double bre);
    	void Height( double hei);
    	void Base (double bas);
    	void Depth (double dep);
    	void Radius(double rad);
    	
    };
    
    //functions begin
    double Volume1::VolumeBox(void)//volume for square box 1 and 2
    {
    	return length * breadth * height;
    }
    double Volume1::VolumeTri(void)//volume for triangle box
    {
    	return 0.5 * base * height * depth; 
    }
    double Volume1::VolumeCyl(void)//volume of cyllinder
    {	
    	return M_PI * radius * radius * height;
    }
    double Volume1::VolumeCone(void)//volume for cone
    {
    	return ( M_PI * M_PI * height ) / 3.0;
    }
    
    
    void Volume1::Length (double len)
    {
    	length = len;
    }
    void Volume1::Breadth (double bre)
    {
    	breadth = bre;
    }
    void Volume1::Height (double hei)
    {
    	height = hei;
    }
    void Volume1::Base (double bas)
    {
    	base = bas;
    }
    void Volume1::Depth (double dep)
    {
    	depth = dep;
    }
    void Volume1::Radius (double rad)
    {
    	radius = rad;
    }
    	
    
    int main()
    {
    	
    
    	Volume1 box1;
    	Volume1 cone2;
    	Volume1 tri3;
    	Volume1 cyl4;
    
    
    double volume = 0.0;
    double volumeCone = 0.0;
    double volumeTri = 0.0;
    double volumeCyl = 0.0;
    	
    int choice;
    
    cout << endl << "for volume of a Box choose 1: "<< endl << endl << "for volume of a Cone choose 2: "<< endl << endl << 
    	"for volume of a Triangle choose 3: "<< endl << endl << "for volume of a Cyllinder choose 4: "<< endl << endl;
    
    
    cin >> choice;
    	if (choice<1||choice>4) {
    		cout << "Please only chose 1, 2, 3 or 4 only";//choice has to 1-4 only
    	}
    	
    switch (choice) {
    	case 1:
    	
    	//box 1 specs
    	cout << "What is your height for the box in inches: " << endl;
    	cin >> box1.height;
    	cout << "What is your lenth for the box in inches: " << endl;
    	cin >> box1.length;
    	cout << "What is your width for the box in inches:  "  << endl;
    	cin >> box1.breadth;
    		//volume of box 1
    		volume = box1.VolumeBox();
    		cout << "Volume of Box = " << volume <<" "<< "cubic inches" << endl << endl;
    		break;
    	
    	case 2:
    	//box 2 specs
    	cout << "What is your height for the cone in inches: " << endl;
    	cin >> cone2.height;
    	cout << "What is your radius for the cone in inches: "  << endl;
    	cin >> cone2.radius;
    		volumeCone = cone2.VolumeCone();
    		cout << "Volume of Cone = " << volumeCone <<" "<< "cubic inches" << endl << endl;		
    		break;
    		
    	case 3:
    	//triangle box
    	cout << "What is your height for the triangle box in inches: " << endl;
    	cin >> tri3.height;
    	cout << "What is your base for the triangle box in inches: "  << endl;
    	cin >> tri3.base;
    	cout << "What is your depth for the box in inches: " << endl;
    	cin >> tri3.depth;
    		//volume of a triangle
    		volumeTri = tri3.VolumeTri();
    		cout << "Volume of Triangle Box = " << volumeTri <<" "<< "cubic inches" << endl << endl;		
    		break;
    		
    	case 4:	
    	//cyllinder
    	cout << "What is your height of the cylllinder in inches: " << endl;
    	cin >> cyl4.height;
    	cout << "What is the radius for the cyllinder in inches: "  << endl;
    	cin >> cyl4.radius;
    		//volume of a cyllinder
    		volumeCyl = cyl4.VolumeCyl();
    		cout << "Volume of Cyllinder = " << volumeCyl <<" "<< "cubic inches" << endl << endl;
    		break;
    	
    	}	
    	
    	
    	return 0;
    }
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by c1!=c2
    Is this what you meant ?
    No, not at all. You would do well to study OOP and OOD more generally rather than C++ specifically - it is possible (in fact likely) to understand all the syntax and semantics of the language and still end up with a poor design if you do not "get" the fundamental concepts.

    You could code relationships that you can clearly express in natural language; such as:

    - box is a kind of shape
    - cylinder is a kind of shape

    or in some applications:

    - box is a kind of volume
    - cylinder is a kind of volume

    may be plausible - if volume is the only attribute of interest perhaps?

    In that context "volume" is a member of "shape" (or "volume") and is inherited by both box and cylinder.

    If volume is the only thing of interest, then the only data member you should have in the base class is "volume" and length, breadth, radius etc. are part of the specialisation sub-classes.

    C++ Code:
     
    class cVolume
    {
        public:
            int volume()
            { 
                return volume ;
            }
     
        protected:
            int m_volume ;
            virtual updateVolume() = 0 ;
    } ;
     
    class cBox : cVolume
    {
        cBox( int l, int d, int h ) : m_length(l), m_depth(d), m_height(h)
        {
            updateVolume()
        }
     
        private :
            int m_length ;
            int m_depth ;
            int m_height ;
     
            int updateVolume()
            {
                m_volume = m_length * m_height * m_depth ;
            }
    } ;
     
    class cCylinder : cVolume
    {
        cCylinder( int l, int d ) : m_length(l), m_diameter(d)
        {
            updateVolume()
        }
     
        private :
            int m_length ;
            int m_diameter ;
     
            int updateVolume()
            {
                m_volume = m_length * M_PI * m_diameter ;
            }
    } ;


    Then you can write code thus:
    C++ Code:
     
    cBox box( 10, 5, 4 ) ;
    cCylinder cylinder( 10, 5 ) ;
    int vb = box.volume() ;
    int vc = cylincer.volume() ;


    Of course this is a somewhat contrived and incomplete example, and cVolume may be an inappropriate base class is "volume" is not the only common attribute of interest.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    4
    i think i see what you mean. I make a main class with virtual in it then i make other classes that inherit from the main class this way i can put my functions inside my classes instead of writing a bunch of functions below the class. I will look up OOP and OOD and read up on them. Thank you for your examples and suggestions.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    4
    I do not get errors until after main so i believe my classes and sub classes are ok. Can you please point me in the right direction. I want to but back in my user input for dimensions would i have to use a switch statement again ? and what would hold my cin user input.
    Code:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    class cVolume //base clase
    {
    public:
    	int volume()
    	{
    		return volume();
    	}
    	
    
    protected:
    	int m_volume;
    	virtual int updateVolume() = 0;
    	
    };
    
    class cBox : cVolume //derived class for box
    {
    	cBox (int l, int d, int h) : m_length(l) , m_depth(d) , m_height(h)
    	{
    		updateVolume();
    	}
    	
    	private :
    	int m_length;
    	int m_depth;
    	int m_height;
    	
    	int updateVolume()
    	{
    		m_volume = m_length * m_height * m_depth;
    	}
    		
    };
    
    class cCylinder : cVolume //derived class for cylinder
    {
    	cCylinder (int l, int d ) : m_length(l) , m_diameter(d)
    	{
    		updateVolume();
    	}
    	
    	private :
    	int m_length;
    	int m_diameter;
    	
    	int updateVolume()
    	{
    		m_volume = m_length * M_PI * m_diameter;
    	}
    	
    };
    
    class cCone : cVolume //derived class for cone
    {
    	cCone (int h, int r) : m_height(h), m_radius(r)
    	{
    		updateVolume();
    	}
    	
    	private :
    	int m_height;
    	int m_radius;
    		
    	int updateVolume()
    	{
    		m_volume = ( M_PI * m_radius * m_radius * m_height )/ 3.0;
    	}
    	
    };
    
    class cTriangle : cVolume //derived class for triangle
    {
    	cTriangle (int b, int h, int d ) : m_base(b) , m_height(h), m_depth(d)
    	{
    		updateVolume();
    	}
    	
    	private :
    	int m_base;
    	int m_height;
    	int m_depth;
    	
    	int updateVolume()
    	{
    		m_volume = 0.5 * m_base * m_height * m_depth;
    	}
    	
    };
    	
    
    int main()// fix from here down
    {
    
    	cBox box(10, 10, 5); // box dimensions
    	int vb = box.volume; // get volume
    	cCylinder cylinder(20, 10);// cylinder dimensions
    	int vc = cylinder.volume;//get volume
    	cCone cone(20, 10);//cone dimensions
    	int vcn = cone.volume;//get volume	
    	cTriangle triangle(10, 12, 20);//dimensions for tri
    	int vt = triangle.volume;//get volume	
      return 0;
    }
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by c1!=c2
    I do not get errors until after main so i believe my classes and sub classes are ok.
    That does not follow; read the error messages carefully and understand them. And rather than force anyone to try to compile your code to see the errors, you might have posted them!

    Your classes do not have a "public:" section - the default access of a class is "private:", so all your constructors are private, and this make it impossible to instantiate an object.

    In:
    Code:
    	int vb = box.volume; // get volume
    volume is a function, not an attribute, you need to call it as a function:
    Code:
    	int vb = box.volume(); // get volume
    similarly for all your other objects.


    Originally Posted by c1!=c2
    Can you please point me in the right direction. I want to but back in my user input for dimensions would i have to use a switch statement again ? and what would hold my cin user input.
    Yes, you could do that, in C++ you can instantiate an object anywhere within a statement block, so you can instantiate the necessary object within the case block right after you have obtained the necessary dimension parameters. It is generally good practice to declare variables and objects at the minimum possible scope - get out of the habit of declaring all variables at the top of a function. That said, you should enclose the entirety of each case block in {} to prevent "cross-case scope" of local variables - you compiler will probably issue a warning otherwise:
    Code:
    switch( x )
    {
        case 1 :
        {  // braces here...
            int y = 0 ;  // ... prevent y from being in scope in subsequent cases.
        }
        break ;
    
        case 2 :
        {
        }
        break ;
    
        default :
        {
        }
        break ;
    }
    If you habitually place braces around case blocks as above, even when not declaring local variables within cases, it will make code maintenance easier.


    Finally include <cmath> rather than <math.h>. Although it is teh C math library, cmath does a couple of useful things: It moves math.h into the std:: namespace, and it provides function overloads for data types other than "double", so there is less need for casting.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0
    Good work Dude, appreciative. Best of luck :-)

    Comments on this post

    • clifford disagrees : Inane an pointless comment not appreciated.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    4
    I am getting a segmentation fault when i run pgm

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    class cVolume //base clase
    {
    public:
    	int volume()
    	{
    		return volume();
    	}
    	
    
    protected:
    	int m_volume;
    	virtual int updateVolume() = 0;
    	
    };
    
    class cBox : public cVolume //derived class for box
    {
        public:
    	cBox (int l, int d, int h) : m_length(l) , m_depth(d) , m_height(h)
    	{
    				updateVolume();
    	}
    	
    	private :
    	int m_length;
    	int m_depth;
    	int m_height;
    	
    	int updateVolume()
    	{
    		m_volume = m_length * m_height * m_depth;
    	}
    		
    };
    
    class cCylinder : public cVolume //derived class for cylinder
    {
    	public:
    	cCylinder (int l, int d ) : m_length(l) , m_diameter(d)
    	{
    		updateVolume();
    	}
    	
    	private :
    	int m_length;
    	int m_diameter;
    	
    	int updateVolume()
    	{
    		m_volume = m_length * M_PI * m_diameter;
    	}
    	
    };
    
    class cCone : public cVolume //derived class for cone
    {
    	public:
    	cCone (int h, int r) : m_height(h), m_radius(r)
    	{
    		updateVolume();
    	}
    	
    	private :
    	int m_height;
    	int m_radius;
    		
    	int updateVolume()
    	{
    		m_volume = ( M_PI * m_radius * m_radius * m_height )/ 3.0;
    	}
    	
    };
    
    class cTriangle : public cVolume //derived class for triangle
    {
    	public:
    	cTriangle (int b, int h, int d ) : m_base(b) , m_height(h), m_depth(d)
    	{
    		updateVolume();
    	}
    	
    	private :
    	int m_base;
    	int m_height;
    	int m_depth;
    	
    	int updateVolume()
    	{
    		m_volume = 0.5 * m_base * m_height * m_depth;
    	}
    	
    };
    	
    
    int main(void)// fix from here down
    {
    
    	
    	
    	cBox box (10,10,10); // box dimensions
    	int vb = box.volume(); // get volume
    	cCylinder cylinder(20,10);// cylinder dimensions
    	int vc = cylinder.volume();//get volume
    	cCone cone(20,10);//cone dimensions
    	int vcn = cone.volume();//get volume	
    	cTriangle triangle(10,12,20);//dimensions for tri
    	int vt = triangle.volume();//get volume	
    	
    	
    		return 0;
    }
    Code:
    Segmentation fault
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    4
    I tried getting my cin numbers assigned to cin >> cBox box and also the cin >> box.volume(); and cin >> vb; but it wont take my cin here is some of my tries
    Code:
    new-host-3:c++ samuelmckenna$ g++ objectInherit.cpp -o objectInherit
    objectInherit.cpp: In function ‘int main()’:
    objectInherit.cpp:103: error: ‘cbox’ was not declared in this scope
    objectInherit.cpp:103: error: expected `;' before ‘box’
    objectInherit.cpp:104: error: no matching function for call to ‘cCylinder::cCylinder()’
    objectInherit.cpp:44: note: candidates are: cCylinder::cCylinder(int, int)
    objectInherit.cpp:42: note:                 cCylinder::cCylinder(const cCylinder&)
    objectInherit.cpp:105: error: no matching function for call to ‘cCone::cCone()’
    objectInherit.cpp:63: note: candidates are: cCone::cCone(int, int)
    objectInherit.cpp:61: note:                 cCone::cCone(const cCone&)
    objectInherit.cpp:106: error: no matching function for call to ‘cTriangle::cTriangle()’
    objectInherit.cpp:82: note: candidates are: cTriangle::cTriangle(int, int, int)
    objectInherit.cpp:80: note:                 cTriangle::cTriangle(const cTriangle&)
    objectInherit.cpp:116: error: redeclaration of ‘cCone cone’
    objectInherit.cpp:105: error: ‘cCone cone’ previously declared here
    new-host-3:c++ samuelmckenna$ g++ objectInherit.cpp -o objectInherit
    objectInherit.cpp: In function ‘int main()’:
    objectInherit.cpp:103: error: ‘cbox’ was not declared in this scope
    objectInherit.cpp:103: error: expected `;' before ‘box’
    objectInherit.cpp:104: error: no matching function for call to ‘cCylinder::cCylinder()’
    objectInherit.cpp:44: note: candidates are: cCylinder::cCylinder(int, int)
    objectInherit.cpp:42: note:                 cCylinder::cCylinder(const cCylinder&)
    objectInherit.cpp:105: error: no matching function for call to ‘cCone::cCone()’
    objectInherit.cpp:63: note: candidates are: cCone::cCone(int, int)
    objectInherit.cpp:61: note:                 cCone::cCone(const cCone&)
    objectInherit.cpp:106: error: no matching function for call to ‘cTriangle::cTriangle()’
    objectInherit.cpp:82: note: candidates are: cTriangle::cTriangle(int, int, int)
    objectInherit.cpp:80: note:                 cTriangle::cTriangle(const cTriangle&)
    objectInherit.cpp:116: error: redeclaration of ‘cCone cone’
    objectInherit.cpp:105: error: ‘cCone cone’ previously declared here
    new-host-3:c++ samuelmckenna$ g++ objectInherit.cpp -o objectInherit
    objectInherit.cpp: In function ‘int main()’:
    objectInherit.cpp:103: error: no matching function for call to ‘cBox::cBox()’
    objectInherit.cpp:24: note: candidates are: cBox::cBox(int, int, int)
    objectInherit.cpp:22: note:                 cBox::cBox(const cBox&)
    objectInherit.cpp:104: error: no matching function for call to ‘cCylinder::cCylinder()’
    objectInherit.cpp:44: note: candidates are: cCylinder::cCylinder(int, int)
    objectInherit.cpp:42: note:                 cCylinder::cCylinder(const cCylinder&)
    objectInherit.cpp:105: error: no matching function for call to ‘cCone::cCone()’
    objectInherit.cpp:63: note: candidates are: cCone::cCone(int, int)
    objectInherit.cpp:61: note:                 cCone::cCone(const cCone&)
    objectInherit.cpp:106: error: no matching function for call to ‘cTriangle::cTriangle()’
    objectInherit.cpp:82: note: candidates are: cTriangle::cTriangle(int, int, int)
    objectInherit.cpp:80: note:                 cTriangle::cTriangle(const cTriangle&)
    objectInherit.cpp:112: error: redeclaration of ‘cBox box’
    objectInherit.cpp:103: error: ‘cBox box’ previously declared here
    objectInherit.cpp:116: error: redeclaration of ‘cCone cone’
    objectInherit.cpp:105: error: ‘cCone cone’ previously declared here
    new-host-3:c++ samuelmckenna$ g++ objectInherit.cpp -o objectInherit
    new-host-3:c++ samuelmckenna$ ./objectInherit
    Segmentation fault
    new-host-3:c++ samuelmckenna$
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    4
    can you please give me a example on how i would get my cin int assigned. i have been working on this since my last post and cant get them cin numbers into my objects. Do i do it in the object itself ? can you give me a example please
  24. #13
  25. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Now is a really good time to familiarise yourself with the debugger.
    Code:
    $ g++ -g foo.cpp
    $ gdb ./a.out 
    GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
    Copyright (C) 2011 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    For bug reporting instructions, please see:
    <http://bugs.launchpad.net/gdb-linaro/>...
    Reading symbols from /home/sc/Documents/a.out...done.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000400873 in cVolume::volume (this=0x7fffffffdf60) at foo.cpp:11
    11          return volume();
    (gdb) bt
    #0  0x0000000000400873 in cVolume::volume (this=0x7fffffffdf60) at foo.cpp:11
    #1  0x0000000000400878 in cVolume::volume (this=0x7fffffffdf60) at foo.cpp:11
    #2  0x0000000000400878 in cVolume::volume (this=0x7fffffffdf60) at foo.cpp:11
    #3  0x0000000000400878 in cVolume::volume (this=0x7fffffffdf60) at foo.cpp:11
    #4  0x0000000000400878 in cVolume::volume (this=0x7fffffffdf60) at foo.cpp:11
    <<snipped>>
    Now in this particular instance, this list goes on for many pages, printing out the same function over and over again.

    What you have here is un-terminated recursion, well at least until the OS steps in because you ran off the end of the stack.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  26. #14
  27. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by c1!=c2
    can you please give me a example on how i would get my cin int assigned. i have been working on this since my last post and cant get them cin numbers into my objects. Do i do it in the object itself ? can you give me a example please
    Both cin and cout work because the >> and << operators are overloaded such that the datatype of the following operand tells the compiler which version of the operator to use. So in order to cin >> into a cBox object, you would need to overload the >> operator to tell it how to do that. It's been quite a while since I've any operator overloading, but it's a common enough practice that you should be able to find information on it easily.

    I was confused about the examples of what you tried. Did you really write them that way?

    cin >> cBox box
    I believe that you would have needed to declare box first. But for cin >> box to have any chance of working, you would still need to overload the operator first.

    cin >> box.volume();
    volume() is a function that returns an int value. You can't assign any input value to a function.

    cin >> vb;
    Since vb is an int variable, then that should have worked. Though it still wouldn't have gotten that value into box.
  28. #15
  29. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    The compiler error log you posted does not seen to relate to either the code you posted or the question you asked immediately before the log. In fact the log appears to be three attempts at compiling the same file with different results (at least in the last attempt) - you must have changed the code. I am really at a loss what this log is for - is it part of your question?

    You are somewhat overloading this thread, and asking question after question without waiting for a response. It will become very difficult to figure out what responses relate to which questions. Consider starting a separate thread for each distinct issue, or at the very least letting people answer one question before posting another!

    With respect to the log, the messages generally mean what they say - you should read them! Start from the top - do not be overwhelmed by the number of errors, most often one error will precipitate another and possibly many, so fix the first one, and if the following ones relate to the code you just changed, ignore them until you have recompiled to see what you have fixed or changed. Alternatively you may have repeated teh same error in multiple places, so the same fix can be applied to all.

    For example, from your log:

    Code:
    objectInherit.cpp: In function ‘int main()’:
    objectInherit.cpp:103: error: ‘cbox’ was not declared in this scope
    Your class was called cBox not cbox - cbox does not exist.

    Code:
    objectInherit.cpp:103:
     error: expected `;' before ‘box’
    Even without seeing the code, this is certainly caused by teh previous error and will be fixed by correcting that.

    Code:
    objectInherit.cpp:104: error: no matching function for call to ‘cCylinder::cCylinder()’
    objectInherit.cpp:44: note: candidates are: cCylinder::cCylinder(int, int)
    objectInherit.cpp:42: note:                 cCylinder::cCylinder(const cCylinder&)
    You have defined two constructors for cCylinder, and none of them take zero arguments. A default constructor is only supplied automatically if you have defined no explicit constructors.

    Code:
    objectInherit.cpp:105: error: no matching function for call to ‘cCone::cCone()’
    objectInherit.cpp:63: note: candidates are: cCone::cCone(int, int)
    objectInherit.cpp:61: note:                 cCone::cCone(const cCone&)
    Ditto for cCone.

    Code:
    objectInherit.cpp:106: error: no matching function for call to ‘cTriangle::cTriangle()’
    objectInherit.cpp:82: note: candidates are: cTriangle::cTriangle(int, int, int)
    objectInherit.cpp:80: note:                 cTriangle::cTriangle(const cTriangle&)
    Ditto for cTriangle.

    Code:
    objectInherit.cpp:116: error: redeclaration of ‘cCone cone’
    objectInherit.cpp:105: error: ‘cCone cone’ previously declared here
    You declared cCone cone at line 105 and then again at the same scope level at line 116.

IMN logo majestic logo threadwatch logo seochat tools logo