In the shape.cpp I need set a default constructor for class Point, But I don't know how to do that.
Thanks
Point::Point(double _f, double _g){
f = 1;
g = 1;
}
Rectangle::Rectangle():Point(1, 1) {
x = 1;
y = 1;
}
Rectangle::Rectangle( Point q, double l, double w):x(l),y(w),Point(q) {
}
#include <iostream>
using namespace std;
class Point{
int f,g; //declaring point(f,g)
public :
Point(); //This will be your default constructor
Point(double _f,double _g); // Constructor for class Point
int getf(){ //just for purpose of display function() below. You can remove this once you understand code.
return f;
}
int getg(){ //just for purpose of display function() below. You can remove this once you understand code.
return g;
}
};
class Rectangle{
double l,w; //declaring variable to store length and width of Rectangle
Point pt; //whatever your use of point is
public :
Rectangle(Point q,double l,double w); //constructor for class Rectangle
void display(){ // displaying the class...just for demonstration
cout<<"l = "<<l<<endl;
cout<<"w = "<<w<<endl;
cout<<"pt = ("<<pt.getf()<<", "<<pt.getg()<<")\n";
}
};
//Defining the constructor of class Point
Point::Point(double _f, double _g):f(_f),g(_g){} // I have used initialiser list here
Point::Point():f(0),g(0){} // (your needed default constructor)
Rectangle::Rectangle( Point q, double l, double w):l(l),w(w),pt(q) {} //Defining the constructor of class Rectangle
int main()
{ //Demonstrating object creation and displaying of object r of class rectangle
Point p(1,2);
Rectangle r(p,5,10);
r.display();
}
I have attached the code that would help you understand about constructors and how to define them.
Hope this solves your question !
class Point{
int f,g;
public:
Point();
};
Point::Point(){
f = 1;
g = 1;
}
int main(){
Point *p = new Point() ;
}
Related
I want to have my grid class constructor to take in drivers_location parameters , but it keeps giving me these errors.
https://imgur.com/a/y4MZqso
#include <iostream>
#include <string>
using namespace std;
class drivers_location {
public:
drivers_location() = default;
drivers_location(string name, float xx, float yy){
x = xx;
y = yy;
name = driver_name;
}
private:
float x{};
float y{};
string driver_name;
};
class grid {
public:
grid() = default;
grid(drivers_location(string name, float xx, float yy));
private:
};
int main() {
drivers_location p;
float pointx{ 2.0 };
float pointy{ 3.0 };
grid m[5];
m[0] = { {"abdul" , pointx, pointy }};
}
I want the grid to take in parameters of drivers_location without using inheritance if that's possible
The correct syntax for declaring a constructor takes argument of type driver_location is as shown below. Note that you don't have to specify the 2 parameters of driver_location when defining the constructor for grid that has a parameter of type driver_location.
class grid {
public:
grid() = default;
//---vvvvvvvvvvvvvvvv---->this is how we specify that this ctor has a parameter of type drivers_location
grid(drivers_location){
//add your code here
}
private:
};
I would also recommend using a good c++ book.
So i created the class Point and want to use it as the parameter of the constructor in the class Circle , but the error : There is no default constructor for class "Point" shows up and I dont know how to fix it. The code is represented below this text:
class Point {
private:
int x, y;
public:
Point(int X, int Y) {
x = X;
y = Y;
}
};
class Circle {
private:
int radius;
Point centre;
public:
Circle(Point q, int r) {
centre = q;
radius = r;
}
};
int main() {
Point obj = Point(3, 4);
Circle obj = Circle(obj, 3);
}
The first problem is that when the constructor Circle::Cirlce(Point, int) is implicitly called by the compiler, before executing the body of that ctor, the data members centre and radius are default initialized. But since you've provided a user-defined ctor Point::Point(int, int) for class Point, the compiler will not synthesize the default ctor Point::Point(). Thus, the data member centre cannot be default initialized.
To solve this you can use constructor initializer list as shown below. The constructor initializer list shown below, copy initialize the data member centre instead of default initializing it.
class Point {
private:
int x, y;
public:
Point(int X, int Y) {
x = X;
y = Y;
}
};
class Circle {
private:
int radius;
Point centre;
public:
//--------------------------vvvvvvvvvvvvvvvvvvvv--->constructor initializer list used here
Circle(Point q, int r): radius(r), centre(q)
{
}
};
int main() {
Point obj = Point(3, 4);
Circle circleObj(obj,4);
}
Demo
Additionally, you had 2 objects with the same name obj inside main.
So I'm a total noob at C++, I decided to learn C++ and skipped directly to the Object-oriented programming. I'm coding a class called KineticEnergy that has a constructor with the parameters x and y which is assigned to the variables mass and velocity.
I have a class method called result() which calculates the Kinetic Energy using its formula. I want to call the parameters from my constructor within the formula but I have no idea what I'm exactly doing here (bad english, don't know how to explain). I am getting errors like "[Error] x was not declared in this scope". Here is the code I written:
#include <iostream>
#include <cmath>
using namespace std;
class KineticEnergy
{
public:
double mass;
double velocity;
KineticEnergy(double x, double y) {
mass = x;
velocity = y;
}
double result()
{
return (1/2) * (x * (pow(y, 2)));
} // What am I gonna do here for this to work?
};
int main()
{
double a = 12.1;
double b = 6.4;
KineticEnergy ke(a, b);
cout << ke.result();
return 0;
}
It is not necessary. your constructor parameters is saved in "mass" and "velocity" as class members.
double result()
{
return (1./2.) * (mass * (pow(velocity , 2.)));
}
Parameters of the parameterized constructor are not member variables. That's why you are storing param values in member variables inside of the parameterized constructor. So that, you should use member variables inside of the result() function.
try this
#include <iostream>
#include <cmath>
using namespace std;
class KineticEnergy
{
public:
double mass;
double velocity;
KineticEnergy(double x, double y) {
mass = x;
velocity = y;
}
double result()
{
return 0.5 * (mass * pow(velocity, 2));
}
};
int main()
{
double a = 12.1;
double b = 6.4;
double Result;
KineticEnergy ke(a, b);
Result = ke.result();
cout << Result;
}
x and y were declared in your constructor, therefore only known by your constructor. you cannot use them outside of it. however, mass and velocity are known variables of your class and can be used anywhere as long as they are public.
in your main you give mass and velocity of your ke object values, that's why you can call any method of your class that uses these variables after(again, as long as they're public)
I've nested a class for use within another class and need to try accessing individual parts of it but can't. How would I go about doing this?
class Point
{
public:
Point() { float x = 0, y = 0; }
void Input(int &count); //input values
Rectangle myRec;
private:
float x, y;
};
class Rectangle
{
public:
Rectangle(); //side1 - horizontal, side2 - vertical
void SetPoint(const Point point1, const Point point2, const Point point3, const Point point4) { LLPoint = point1; LRPoint = point2; URPoint = point3; ULPoint = point4; }
float CalcSides(Point LL, Point LR, Point UL, Point UR);
private:
Point LLPoint, LRPoint, ULPoint, URPoint;
float side1, side2, length, width, area, perimeter; //side1 - horizontal, side2 - vertical
};
float Rectangle::CalcSides(Point LL, Point LR, Point UL, Point UR)
{
side1 = (LR.x - LL.x);
}
How can I access the x and y values for the points I've created in the Rectangle class?
If you really want to do this, then you can make the classes friends.
class Rectangle;
class Point
{
friend class Rectangle;
public:
Point() { x = 0; y = 0; }
void Input(int &count); //input values
private:
float x, y;
};
More likely though, you simply want to add accessors to the Point class as it is fairly useless as is.
class Point
{
public:
Point() { x = 0; y = 0; }
void Input(int &count); //input values
float getX() const { return x; }
float getY() const { return y; }
private:
float x, y;
};
Or, if Point is really going to be so simple and not need to maintain any invariants at all, just expose x and y as public members.
Also, you probably don't want to have Point contain a Rectangle but rather refer to one either through a pointer or a reference, if it refers to one at all. After all, a Point can be useful without reference to a Rectangle (e.g. - maybe it's used for Triangles too).
I need help building a constructor that would initialized the respective data when instantiated within
the main().
#include <iostream>
using namespace std;
class Entity{
public:
int x, int y, char icon; };
int main(){
Entity pData;
pData.x=4; pData.y=3, pData.icon='1';
cout<<pData.x<<'\n'\; cout<<pData.y<<'\n'\; cout<<pData.icon<<\'n'\;
}
I included an example of what i need only... there no need to include all the program. Anyways I need the constructor to initialized the data in the main as soon as the instance(pData) of Entity is created: I know the constructor has to be something like
Entity::Entity(int x, int y, char icon){};
and once instantiated in the main it would be something like
Entity pData{3,4,'1'};
but obviously this isn't working for me
oh by the way I need a constructor because that's what the assignment is asking in the first place here you go copied right off from the doc file
"write a parameterized constructor for the Entity class that sets x, y, and icon, and use it when creating the instance"
Actually u have not defined constructor for your class Entity(But compiler have defined it for you but it only allocate memory for member variable of Entity).
class Entity {
public:
int x,y;
char icon;
Entity(int _x, int _y,char _icon)
{
x=_x;
y=_y
icon=_icon;
}
};
int main()
{
Entity obj(4,3,'I');
return 0;
}
`
Entity::Entity(int a, int b, char c)
{
x = a;
y = b;
icon = c;
};
Define your constructor like this...
And call it like:-
Entity pData( 1,2,'a');
You don't need to add a constructor because your class is an aggregate. This would work (after fixing some typos)
class Entity
{
public:
int x, y;
char icon;
};
#include <iostream>
int main()
{
using std::cout;
Entity pData{3,4,'1'};
cout << pData.x <<'\n';
cout << pData.y <<'\n';
cout << pData.icon <<'\n';
}
You can for example define a constructor the following way
class Entity{
public:
Entity( int x, int y, char icon ) : x( x ), y( y ), icon( icon ) {}
int x, int y, char icon;
};
And use it like
Entity pData( 4, 3,'1' );
or
Entity pData { 4, 3,'1' };
or
Entity pData = { 4, 3,'1' };
Take into account that for your original class definition you could write
Entity pData = { 4, 3,'1' };
without defining explicitly a constructor.