Ask your own question, for FREE!
Computer Science 23 Online
OpenStudy (anonymous):

Can someone explain me the "Function Overloading and Function Overriding" in C++(chapter inheritance). Can you give me some examples who it works? I have read in books, i cant understand. pls help

OpenStudy (turingtest):

Your question is fairly vague, but basically function overloading is when you make two functions which have the same name perform different operations by giving them different arguments. For example say we want a function to compute the area of a shape. Computing the shape of a circle requires only one input: the radius. So an area function for a circle would take one parameter, like so: double area(double radius) { return 3.14 * radius; } we can "overload" this function to find the area of other shapes by changing the number of parameters it takes. For instance, a rectangle's area is determined by two parameters: height and width, so in the same program or class declaration we can also write another function called "area" that takes two formal parameters. Example: double area(double width, double height) { return width * height; } Now if we call this function in main, the compiler will know which version of the area function we want based on how many parameters we give it. int main() { double circle, rectangle; double x = 1, y = 2; circle = area(x); rectangle = area(x, y); printf("The area of the circle with radius %d is %d\n", x, circle); printf("The area of the rectangle with width %d and height %d is %d\n", x, y, rectangle); return 0; } This program will output the area of each shape. The compiler knew which area function you wanted based on the number and types of parameters you passed to it. When you passed one parameter, x, the compiler used the area function that had only one parameter. When you passed in both x and y, the compiler used the area function that accepts two parameters. This is this basic idea of function overloading.

OpenStudy (anonymous):

@TuringTest I have a question. A class Shape can be used to represent geometrical shapes and has: attributes: 1. X_Center to represent the x-coordinate of its center 2. Y_Center to represent the y-coordinate of its center. 3.area to represent its area. member functions: 1.a constructor that takes as parameters, 2 floating point values(X_Val and Y_Val) and assign them to the attributes X_Center and Y_Center and also initalizes the value to area to 0.0 3. a pure virtual method getArea(), to calculate and return the area of a given shape. 4.a virtual method display to display the center and area of a given shape. Quest1: Write down the interface of the class shape. class Shape: { private: float X_Center; float Y_Center; float area; public: Shape(float, float); void getArea(); Quest2:Implement the constructor for the class shape can you help me in this.

OpenStudy (anonymous):

Solution for Quest2: #include<iostream> #include "Shape.h" using namespace std; Shape::Shape(float X_Val, Y_Val) { X_Center=X_Val; Y_Center=Y_Val; float Shape::getArea() { return area; } Pls correct me.

OpenStudy (anonymous):

how to calculate the area?

OpenStudy (turingtest):

sorry I misread, area is an attribute I'm not seeing anything wrong just yet, except you're missing some curly braces after your constructor definition

OpenStudy (turingtest):

what error are you getting?

OpenStudy (turingtest):

oh you forgot to put "float" on the second formal parameter in your constructor

OpenStudy (turingtest):

class Shape: { private: float X_Center; float Y_Center; float area; public: Shape(float, float); void getArea(); } Shape::Shape(float X_Val, float Y_Val) { X_Center=X_Val; Y_Center=Y_Val; } float Shape::getArea() { return area; }

OpenStudy (anonymous):

i was not sure of my answer, i wanted you to confirm if everything is correct. I am happy that i have been able to write this by my own. Yeah i missed some curly braces and forgot to declare float for getArea();

OpenStudy (turingtest):

yeah looks good to me :)

OpenStudy (anonymous):

but the question still continues: A class Circle inherits from the class Shape(using public access) and has an additional attribute radius to represent the radius of a circle. The interface of the class Circle is given below: #include "Shape.h" class Circle:public Shape { private: float radius; public: circle(float, float, float); float getArea(); void display(); }; The constructor of the class Circle accepts 3 floating point values and set the attributes X_Center, Y_Center and radius using these values. The method getArea() calculates and returns the area of the Circle and the method display() displays the attributes of a circle. Q3: Implement the class Circle.

OpenStudy (anonymous):

#include<iostream> #include "Shape.h" using namespace std; Circle::Circle(float X_Center, float Y_Center, float rad):Shape(X_val, Y_val) { radius=rad; } area=3.14*radius; float Circle::getArea() { return area; } void Circle::display() { Shape::display(); cout<<"Radius of circle:"<<endl; } Can you correct me. for the interface Shape, i had not include a display method, should there be a display method. The member functions says that a virtual method display to display the center and area of a given shape. ?

OpenStudy (turingtest):

your program is pretty good, but your display function seems to inherit from some display function from shape, which you never implemented. Also area = 3.14 * radius * radius (I made the same typo earlier too :P) You shouldn't alter the area attribute either, since the specifications tell you that "The method getArea() calculates and returns the area of the Circle", and your program tries to calculate the area ahead of time outside of the getArea() function. I actually haven't done much with inheritance in C++; I've spend more time with OOP in python, but I'd venture something like void Shape::display() { cout << "The coordinates are " << X_Center << ", " << Y_Center << endl; } // I'd put this your shape class declaration (in Shape.h) Circle::Circle(float X_Center, float Y_Center, float rad):Shape(X_val, Y_val) { radius=rad; } float Circle::getArea() { return 3.14 * radius * radius; } void Circle::display() { Shape::display(); cout<<"Radius of circle:" << radius <<endl; cout << "Area of cirlce: " << getArea() << endl; } however I'm not 100% sure on this. Like I said, I haven't played around with inheritance much yet in C++. I hope it at least helped though

OpenStudy (anonymous):

@TuringTest yeah this has helped me for sure. By the way, some questions. Should i post it separately.? or if u want, i post it here?

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!