Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Rectangle(float a):Shape(a) {};  //!3! Missing Constructor

why we need this??

What I have tried:

#include<iostream>
using namespace std;
class Shape
{
public:                     //!1! Constructor must be public!
	Shape(float a)
	{
		w = a;
	}
	void get()              //!2! Member hast to be public
	{
		cin >> w >> h;
	}
protected:
	float w, h;
};

class Rectangle : public Shape
{
public:
	Rectangle(float a):Shape(a) {};  //!3! Missing Constructor
// protected:                   //!4! Member hast to be public
	void area()                 //!5! missing brackets
	{
		cout << w*h << endl;
	}
};

int main()
{
	Rectangle R(1.0);     //!6! spelling wrong + constructor mismatch
	R.get();
	R.area();
	return 0;
}
Posted
Updated 9-Aug-22 6:25am
Comments
Richard Deeming 9-Aug-22 8:44am    
Because you do.

Seriously, an unexplained code-dump, some cryptic comments, and a single line of "why we need this?" does not make for a good question.

If you want a proper answer, click the green "Improve question" link and update your question to include a clear and complete description of the problem you are trying to solve, what you have tried, and where you are stuck.
Richard MacCutchan 9-Aug-22 11:52am    
The posted c ode works correctly. So none of the comments are valid.
merano99 9-Aug-22 12:12pm    
He copied my answer to a question and the comments mark where it was wrong before. Apparently he copied without understanding it.
Richard MacCutchan 9-Aug-22 12:29pm    
Sadly not an unusual event.

If I copy and paste your code into an online compiler - onlinegdb.com - it compiles with no errors, and runs. It doesn't tell the user what to input, or even that he should input anything, but it works: I give it "2.6 4.8" and it gives me "12.48".

So what is the problem? At a guess, your teacher has added comments saying what he doesn't like, and you have fixed some of them. Which ones you think you have fixed, we have no idea ...
 
Share this answer
 
Comments
merano99 9-Aug-22 12:14pm    
The problem seems to be that he does not understand the code he copied from my answer. It really would have been better if I had left out the mistakes and just commented on what didn't fit. Unfortunately, he didn't correct anything himself, not even the typos.
We had the code commented on 1-Aug-22 in response to N Hasan's question.

Quote:
Need to explain all the cases according to the errors

https://www.codeproject.com/Answers/5338717/Need-to-explain-all-the-cases-according-to-the-err#answer2

It's amazing how little personal effort goes into this question.


//Edit:
I can briefly touch on the basic context. However, it would be clearly necessary to read this in a basic book.

The Rectangle class inherits from the Shape class. The data is protected and is neither inherited nor is it directly modifiable via the Rectangle class. The class Shape has only one constructor, which has a float parameter. So the parameter is needed when creating an instance of the class.
C++
class Shape {
public: 
	Shape(float a);
	...
protected:
	float w, h;
};

class Rectangle : public Shape

If you want to create an instance of the class Rectangle, the constructor of Shape is called, too. Now there are some questions.
1. where does the constructor of Shape get the parameter?
2. how are the protected parameters initialized?

In initialization lists, inherited properties cannot be accessed, therefore the properties are initialized by calling the corresponding constructor of the parent class.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900