answersLogoWhite

0

#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

10y ago

What else can I help you with?

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'


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).


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.

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.


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

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


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).


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.


How do you print from compaq laptop?

Use the option File / Print in the program you are using.