answersLogoWhite

0


Best Answer

#include<iostream>

struct point

{

int x;

int y;

double length (const point& p) const;

};

double point::length (const point& p) const

{

int w = x - p.x;

int h = y - p.y;

return std::sqrt ((w*w) + (h*h));

}

struct rectangle

{

size_t width;

size_t height;

};

struct triangle

{

point A, B, C;

double length_a () const { return B.length (C); }

double length_b () const { return A.length (C); }

double length_c () const { return A.length (B); }

double perimeter () const { return length_a() + length_b() + length_c(); }

};

size_t area (triangle& t)

{

const double s = t.perimeter() / 2;

return std::sqrt (s * (s - t.length_a()) * (s - t.length_b()) * (s - t.length_c()));

}

size_t area (rectangle& r)

{

return r.width * r,height;

}

int main()

{

triangle t {{0,10},{10,0},{0,0}};

rectangle r {10,5};

std::cout << "Triangle area: " << area(t) << std::endl;

std::cout << "Rectangle area: " << area(r) << std::endl;

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

#include<iostream>

class rectangle

{

int width;

int height;

public:

rectangle(int w, int h):width(w),height(h){}

int get_area()const{return(width*height);}

};

int main()

{

rectangle rect(3,5);

std::cout<<"Area = "<<rect.get_area()<<std::endl; // Area = 15

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

#include<iostream>

#include<string>

#include<sstream>

const double& min(const double& a, const double& b) {

return(a<b?a:b);}

const double& max(const double& a, const double& b) {

return(b<a?a:b);}

struct rectangle

{

rectangle(double t, double l, double b, double r):top(min(t,b)),left(min(l,r)),bottom(max(t,b)),right(max(l,r)) {}

double width()const{return(right-left);}

double height()const{return(bottom-top);}

double area()const{return(width()*height());}

double perimeter()const{return((width()+height())*2);}

private:

double top, left, bottom, right;

};

double string_to_double( const std::string& s )

{

std::istringstream i(s);

double x;

if(!(i >> x))

return(0.0);

return(x);

}

// Returns 0.0 if the response is unrecognised.

double ask(std::string prompt)

{

std::cout<<prompt<<":\t";

std::string response;

std::cin>>response;

return(string_to_double(response));

};

int main()

{

std::cout<<"Enter the 4 coordinates for a rectangle (top, left, bottom, right).\n"<<std::endl;

double t=ask("Top coordinate");

double l=ask("Left coordinate");

double b=ask("Bottom coordinate");

double r=ask("Right coordinate");

rectangle rect(t,l,b,r);

std::cout<<std::endl;

std::cout<<"Width of rectangle:\t"<<rect.width()<<std::endl;

std::cout<<"Height of rectangle:\t"<<rect.height()<<std::endl;

std::cout<<"Area of rectangle:\t"<<rect.area()<<std::endl;

std::cout<<"Perimiter of rectangle:\t"<<rect.perimeter()<<std::endl;

std::cout<<std::endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

There is no such concept as an overloaded class in C++. Only functions can be overloaded. Assuming you have a rectangle class with width and height members, you can encapsulate a get_area member method that returns the product of the width and height members. There is no need to overload anything to achieve this.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

templateclass Rect {

T height;

T width;


public:

Rect(T h, T w) {this->height = h; this->width=w;}

T area() {return this->height * this->width;}

}



int main() {


Rect r(10.5, 5.25);

float area = r.area();


cout << "Area = " << area << endl;


return 0;


}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

chup bay ruk ghajini mega movie ajab prem ki ghzab kahani freedom ryt now na na belly dancer no matter togethre aviel labivmnvm dostijej octaviro laoki

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

size_t rectangle_perimeter (const size_t width, const size_t height) { return (width+height) * 2; }

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a C plus plus program to print the area of rectangle using overloading class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why you use overloading and overriding?

Overloading the same method name with different number of arguments (and the data types), and perhaps with a different returned data type. The method signatures are different, only the names are the same. Overriding is to change the same method name with different implementation (the method body). The method signature stays the same.


Is it possible for your Alice program to have more than one instance in a class?

It is possible to have more than one instance of the same class, because the class is simply the blue print for the actual object.


Write a unix program to print print a pattern?

echo 'print a pattern'


What is the difference between overloading and overriding and polymorphism in terms of C plus plus?

In C++, overloading, overriding, and polymorphism have the following meanings...Overloading is when you create two functions of the same name, but with different argument types, or with a different number of arguments. The compiler generates code for each case. This can be done inside or outside a class. Operators can also be overloaded, as they are (for all practical purposes) functions, but operators can only be overloaded in the context of a class.Overriding is when you derive a child class from a base class, and you replace a method (function) of the base class with a method of the child class using the same type and number of arguments. This is not overloading - it is redeclaration - and the overridden method only applies when dealing with an instance of the child class.Polymorphism is the same as overriding, except that you declare any base method virtual, making all base and derived methods virtual. Virtual, in the context of a class, means to create a table in the static portion (all instances) of the class that point to the specific overridden variants of the methods of the class. This allows you to declare a pointer to the base class, initialize it with the address of an instance of either the base class or any of the child classes, invoke a method of the class, and have the proper binding determined at run-time.


Write various ways to implement stack data structure?

2. Write a program using switch statement that reads a character representing a geometrical figure, then asks the user to enter the required data (ex. Radius for a circle, length and height for a rectangle, etc. ...) . The program should then print the area and circumference.Figures are: circle(c), square(s), rectangle(r), triangle (t).

Related questions

Write a program that prompts the user to input the length and width of a rectangle and then prints the rectangle's area and perimeter?

PRINT "Give me the rectangle's length.": Input L PRINT "Give me the rectangle's width.": Input W PRINT "The rectangle's area is "; L x W PRINT "The rectangle's perimeter is "; 2 x (L + W) PRINT "You've been a great audience. I'm here til Thursday. Don't forget to tip your waiter. Have a nice day."


Write a program to accept length and breadth of a rectangle and then calculate its area and print it?

int length int breadth int area= (length x breadth) print area


Why you use overloading and overriding?

Overloading the same method name with different number of arguments (and the data types), and perhaps with a different returned data type. The method signatures are different, only the names are the same. Overriding is to change the same method name with different implementation (the method body). The method signature stays the same.


Is it possible for your Alice program to have more than one instance in a class?

It is possible to have more than one instance of the same class, because the class is simply the blue print for the actual object.


Write a unix program to print print a pattern?

echo 'print a pattern'


What is the shape of a rectangle print?

a rectangle. it has four parallel sides each side is the same length as the one on the other side.


What is the difference between overloading and overriding and polymorphism in terms of C plus plus?

In C++, overloading, overriding, and polymorphism have the following meanings...Overloading is when you create two functions of the same name, but with different argument types, or with a different number of arguments. The compiler generates code for each case. This can be done inside or outside a class. Operators can also be overloaded, as they are (for all practical purposes) functions, but operators can only be overloaded in the context of a class.Overriding is when you derive a child class from a base class, and you replace a method (function) of the base class with a method of the child class using the same type and number of arguments. This is not overloading - it is redeclaration - and the overridden method only applies when dealing with an instance of the child class.Polymorphism is the same as overriding, except that you declare any base method virtual, making all base and derived methods virtual. Virtual, in the context of a class, means to create a table in the static portion (all instances) of the class that point to the specific overridden variants of the methods of the class. This allows you to declare a pointer to the base class, initialize it with the address of an instance of either the base class or any of the child classes, invoke a method of the class, and have the proper binding determined at run-time.


Write an assembly language program to print a to z on screen?

write a program to print A to Z on screen in c?


What is method overloading?

Method overloading is when you have multiple methods in a class that have the same name but a different signature.Ex:public void print (String s){}public void print(int a) {}If we use more than one same type of method but different parameters or parameter list with same no. of parameters within same class than it is called methodoverloading.if we use more than one different type of method within same class than we dont call it method overloadingfor example:public void add(int a , int b){systemout.println(a+b);}public void add(double a , double b){systemout.println(a+b);}public void add(string a , string b){systemout.println(a+b);}


Blue print for a house if the bedroom is a parallelogram with at least one right angle the bedroom is a rectangle square or rhombus?

A rectangle (or square).


Which is a program embedded in firmware that manages print jobs?

Print Server


Write various ways to implement stack data structure?

2. Write a program using switch statement that reads a character representing a geometrical figure, then asks the user to enter the required data (ex. Radius for a circle, length and height for a rectangle, etc. ...) . The program should then print the area and circumference.Figures are: circle(c), square(s), rectangle(r), triangle (t).