hellp

Suppose a class polygon has been defined as follows:
1. Data member defined to be available to descendants
• The length coordinate of the polygon.
• The width coordinate of the polygon.
• Print info() declare as Constant, to print (l,w) coordinate explicitly; use the dereferences this pointer and the dot operator to access the member.

Polygon

“ constructor ”+ polygon(l:int, w: int)
+~Polygon()
+set dimension (l:int,w:int)
+getlength():int “const”
+getwidth():int “const”
+print info()

• Declare friend function, which set the values of length & width Coordinate.










-Define a class rectangle inherits from class polygon that contains the methods/data members below and any other necessary methods.
Rectangle

“ constructor ”+ Rectangle(l:int, w: int)
+area():int
+printinfo()

-Define a class Triangle that inherits from class polygon; that contains the methods/data members as following:
Triangle
-height : int
“ constructor ”+ Triangle(w: int,int h)
+setheight(h : int )
+getheight ():int “const”
+printinfo()
+area():int


-Write main code in order to test your work with:
A. constant object
B. non constant object
Hint:
Rectangle area=(length*width)
Triangle area= (length*height)/2


i wish at least you give me the main idea and how suppose it is work
Please note, that it is not a homework site. We won't do your homework for you. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attemts to solve this youself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again. As it is impossible to find deriviative of function without knowledge in ariphmetics, you cannot do more complex tasks in programming without clear understanding of basics

Your assigment clearly states what do you need to do. It deals with basic stuff you should know if you attend any lessons.

And please, do not crosspost your questions across different subforums. It clutters them; annoys people and makes them less likely to help you.
i did something but i was working on it and i know that it is my H.W but im a little confuse



#include <iostream>
using namespace std;

class Polygon
{
protected:
int width, heigth;

public:
void setWidthAndHeigth(int w, int h)
{
width = w;
heigth = h;
}
};

class Rectangle: public Polygon
{
public:
int area()
{
return (width * heigth);
}
};

int main() {

Rectangle rect;

rect.setWidthAndHeigth(33, 12);

cout << "the Area: " << rect.area() << endl;

return 0;
}
1. Data member defined to be available to descendants
• The length coordinate of the polygon.
• The width coordinate of the polygon.
1
2
3
4
5
6
class Polygon
{ //Start of polygon declration
protected:
    int length;
    int width;
public:
“ constructor ”+ polygon(l:int, w: int)
1
2
3
4
5
Polygon(int l, int w)
{
    length = l;
    width = w;
}
+set dimension (l:int,w:int)
1
2
3
4
5
void set_dimensions(int l, int w)
{
    length = l;
    width = w;
}
+getlength():int “const”
+getwidth():int “const”
1
2
int get_length() const {return length;}
int get_width() const {return width;}
+print info()
• Print info() declare as Constant, to print (l,w) coordinate explicitly; use the dereferences this pointer and the dot operator to access the member.
1
2
3
4
void print_info() const 
{
std::cout << '(' << (*this).lenth << ',' << (*this).width << ')';
}
• Declare friend function, which set the values of length & width Coordinate.
If I got it right it should be something like:
1
2
3
4
5
6
7
8
friend void coordinate(Polygon& it, int l, int w);
}; //end of Polygon declaration

void coordinate(Polygon& it, int l, int w)
{
    it.length = l;
    it.width = w;
}
I have did this !!


#include <iostream>
using namespace std;

class polygon
{
protected:
int width, length;

public:
polygon (int w, int l);
void set_dimensions (int w , int l);
int getlength (); const
int getwidth (); const
void printinfo (); const
};



for .h file

and for .cpp file

#include <iostream>
#include "polygon.h"
using namespace std ;

polygon::polygon (int w=0, int l=0)
{
length=l;
width=w;
}

void::polygonset_dimensions (int w, int l)
{
length=l;
width=w;
}

int::polygon getlength () const
{ return length; }

int::polygon getwidth () const
{ return width; }

void::polygon printinfo () const
{
cout << '(' << (*this).length << ',' << (*this).width << ')'<<endl;

}
friend void coordinate(Polygon& it, int l, int w);
}; //end of Polygon declaration

void coordinate(Polygon& it, int l, int w)
{
it.length = l;
it.width = w;
}

but i found some mistakes and i dunno why ? will you help me
and for friend class
should i declare it in another class ? or what
#include <iostream>
#include "polygon.h"
using namespace std ;

class rectangle: public polygon
{
public:
rectangle (int w, int l);
int area ();
void printinfo ();



};



____________________________


#include <iostream>
#include "polygon.h"
#include "rectangle.h"
using namespace std ;


rectangle::rectangle (int w, int l)
{
length=l;
width=w;
}

int::rectanglearea ()
{
return (length*width);
}
void::rectangleprintinfo ()
{}


what should i write on print function ?
#include <iostream>
#include "polygon.h"
using namespace std ;


class tringle: public polygon
{
private:
int height;
public:
tringle (int w , int h);
void setheight (int h);
int getheight (); const
void printinfo ();
int area ();


};

_______________________________________


#include <iostream>
#include "polygon.h"
#include "tringle.h"
using namespace std ;


tringle::tringle (int w , int h)
{

width=w;
height=h;
}

void::tringlesetheight (int h)
{ height=h; }

int::tringlegetheight () const
{ return height; }

int::tringlearea ()
{
return (length*height)/2 }


void::tringleprintinfo ()
{}


there are also mistakes :(
Finally, i have to -Write main code in order to test my work with:


A. constant object
B. non constant object
Topic archived. No new replies allowed.