How to use c++ class objects from .mm file in other file? - c++

I have made two files that are MathUtils.h
#include "iostream"
and MathUtils.cpp
#include "MathUtils.h"
using namespace std;
//Box class .....................
class Box
{
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public:
void setParameters(int l,int b,int h);
int volume();
int area();
};
void Box::setParameters(int l, int b, int h)
{
length=l;
breadth=b;
height=h;
}
int Box::volume()
{
return length*breadth*height;
}
int Box::area()
{
return (2*(length*breadth) + 2*(breadth*height) + 2*(height*length));
}
//sphere class................
class Sphere
{
private:
double pi=3.14;
double r;
public:
void setParameters(int radius);
int volume();
int area();
};
void Sphere::setParameters(int radius)
{
r=radius;
}
int Sphere::volume()
{
return (4/3)*pi*r*r;
}
int Sphere::area()
{
return 4*pi*r*r;
}
How can we use this file in my project could any one help me.I have never use c++ files in my project so I want to know how can we use Box and Sphere class object in other viewController file.
Thanks!.

You define your classes in the .h file.
For your example, move:
class Box
{
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public:
void setParameters(int l,int b,int h);
int volume();
int area();
};
class Sphere
{
private:
double pi=3.14;
double r;
public:
void setParameters(int radius);
int volume();
int area();
};
to mathutils.h and add #include "mathutils.h" to your viewController file. Your member functions for Box should still be in mathutils.c
A function in your view controller can then make use of it:
{
Box b;
b.setParameters(1,2,3);
int v = b.volume();
int a = b.area();
}

Related

Why does the compiler report such an error?('width' was not declared in this scope)

I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
I've tried multiple variations of this, but none of them seem to work. Any ideas?
#include <iostream>
using namespace std;
class RectangleArea
{
public:
void SetData (float L,float W);
float ComputeArea();
void OutputArea();
private:
float length,width,area;
};
void RectangleArea::SetData(float L,float W)
{
length=L;
width=W;
}
float RectangleArea::ComputeArea()
{
area=length*width;
}
void RectangleArea::OutputArea()
{
cout<<"Area is "<<area<<endl;
}
int main()
{
RectangleArea Rectangle;
cout<<"input length and width:"<<endl; //The error appears in this position//
cin>>length>>width;
Rectangle.SetData(length, width);
Rectangle.ComputeArea();
Rectangle.OutputArea();
return 0;
}

Getting undefined refrence in the copy constructors C++

Hello i am trying to create a simple program in C++ and I have problems in linking the files. The error is undefined reference to my copy constructors in the classes. I checked the linking and I can't find any errors in the header file.
I tried messing around with the copy constructors but still no solution. when i comment the copy constructors, the program compiles.
Error message:
/tmp/cc5KI4Dx.o: In function `CenteredShape::CenteredShape(CenteredShape const&)':
Shapes.cpp:(.text+0x1c0): undefined reference to `Shape::Shape()'
/tmp/cc5KI4Dx.o: In function `RegularPolygon::RegularPolygon(RegularPolygon const&)':
Shapes.cpp:(.text+0x258): undefined reference to `CenteredShape::CenteredShape()'
/tmp/cc5KI4Dx.o: In function `Circle::Circle(Circle const&)':
Shapes.cpp:(.text+0x388): undefined reference to `CenteredShape::CenteredShape()'
/tmp/cc5KI4Dx.o: In function `Rectangle::Rectangle(Rectangle const&)':
Shapes.cpp:(.text+0x42c): undefined reference to `RegularPolygon::RegularPolygon()'
/tmp/cc5KI4Dx.o: In function `Square::Square(Square const&)':
Shapes.cpp:(.text+0x576): undefined reference to `RegularPolygon::RegularPolygon()'
collect2: error: ld returned 1 exit status
testshapes.cpp
#include "Shapes.h"
int main(int argc, char** argv) {
Circle c("first circle", 3, 4, 7);
RegularPolygon r("TRIANGLE", 1, 1, 3);
r.printName();
c.printName();
}
Shapes.h
/*
Classic shape examples: an inheritance tree in a geometric context
*/
#ifndef __SHAPES_H
#define __SHAPES_H
#include <string>
class Shape { // base class
private: // private access modifier: could also be protected
std::string name; // every shape will have a name
public:
Shape(const std::string&); // builds a shape with a name
Shape(); // empty shape
Shape(const Shape&);
// copy constructor
void printName() const ; // prints the name
//setters
void setName(std::string newName);
//getters
std::string getName();
};
class CenteredShape : public Shape { // inherits from Shape
private:
double x,y; // the center of the shape
public:
CenteredShape(const std::string&, double, double); // usual three constructors
CenteredShape();
CenteredShape(const CenteredShape&);
//setters
void move(double, double); // moves the shape, i.e. it modifies it center
//getters
double getX();
double getY();
};
class RegularPolygon : public CenteredShape { // a regular polygon is a centered_shape with a number of edges
private:
int EdgesNumber;
public:
RegularPolygon(const std::string&, double, double, int);
RegularPolygon();
RegularPolygon(const RegularPolygon&);
//setters
void setEdgesNumber(int newEdgesNumber);
//getter
int getEdgesNumber();
};
class Circle : public CenteredShape { // a Circle is a shape with a center and a radius
private:
double Radius;
public:
Circle(const std::string&, double, double, double);
Circle();
Circle(const Circle&);
//methods
double perimeter();
double area();
//setters
void setRadius(double newRadius);
//getters
double getRadius();
};
class Rectangle : public RegularPolygon { // a Rectange is a shape with edges
private:
double length;
double width;
public:
Rectangle(const std::string& n, double nx, double ny, double nwidth,double nheight);
Rectangle();
Rectangle(const Rectangle&);
//methods
double perimeter();
double area();
//setters
void setLength(double newLength);
void setWidth(double newWidth);
//getters
double getLength();
double getWidth();
};
class Square : public RegularPolygon { // a Square is a shape with edges
private:
double side;
public:
Square(const std::string& n, double nx, double ny, double nside);
Square();
Square(const Square&);
//methods
double perimeter();
double area();
//setters
void setSide(double newSide);
//getters
double getSide();
};
#endif
Shapes.cpp
// please refer to shapes.h for methods documentation
#include <iostream>
#include "Shapes.h"
using namespace std;
//------------------------------------------------------
Shape::Shape(const string& n) : name(n) {
}
// copy constructor
Shape::Shape(const Shape &shp)
{
name = shp.name;
}
void Shape::printName() const {
cout << name << endl;
}
void Shape::setName(string newName)
{
name = newName;
}
string Shape::getName()
{
return name;
}
//---------------------------------------------------------
void CenteredShape::move(double nx, double ny)
{
x=nx;
y=ny;
}
double CenteredShape::getX()
{
return x;
}
double CenteredShape::getY()
{
return y;
}
CenteredShape::CenteredShape(const string &n, double nx, double ny): Shape(n) {
x = nx;
y = ny;
}
CenteredShape::CenteredShape(const CenteredShape &cshp)
{
x=cshp.x;
y=cshp.y;
}
//-----------------------------------------------------------------------------------
RegularPolygon::RegularPolygon(const string& n, double nx, double ny, int nl) : CenteredShape(n,nx,ny)
{
EdgesNumber = nl;
}
RegularPolygon::RegularPolygon(const RegularPolygon &regp)
{
EdgesNumber=regp.EdgesNumber;
}
void RegularPolygon::setEdgesNumber(int newEdgesNumber)
{
EdgesNumber=newEdgesNumber;
}
int RegularPolygon::getEdgesNumber()
{
return EdgesNumber;
}
//----------------------------------------------------------------------------------
double Circle::perimeter()
{
return (2*3.14*Radius);
}
double Circle::area()
{
return(3.14*Radius*Radius);
}
void Circle::setRadius(double newRadius)
{
Radius=newRadius;
}
double Circle::getRadius()
{
return Radius;
}
Circle::Circle(const string& n, double nx, double ny, double r) : CenteredShape(n,nx,ny)
{
Radius = r;
}
Circle::Circle(const Circle &cr)
{
Radius=cr.Radius;
}
//------------------------------------------------------------------------------------
Rectangle::Rectangle(const string &n, double nx, double ny, double nwidth, double nheight) : RegularPolygon(n,nx,ny,4)
{
length=nheight;
width=nwidth;
}
Rectangle::Rectangle(const Rectangle &rect)
{
length=rect.length;
width=rect.width;
}
double Rectangle::perimeter()
{
return (2*(length+width));
}
double Rectangle::area()
{
return (length*width);
}
void Rectangle::setLength(double newLength)
{
length = newLength;
}
void Rectangle::setWidth(double newWidth)
{
width = newWidth;
}
double Rectangle::getLength()
{
return length;
}
double Rectangle::getWidth()
{
return width;
}
//------------------------------------------------------------------------------
Square::Square(const string& n, double nx, double ny, double nside):RegularPolygon(n,nx,ny,4)
{
side=nside;
}
Square::Square(const Square &sqr)
{
side = sqr.side;
}
double Square::perimeter()
{
return (side*4);
}
double Square::area()
{
return (side*side);
}
void Square::setSide(double newSide)
{
side=newSide;
}
double Square::getSide()
{
return side;
}

Debugging a Derived Class C++

When I run this code and create an instance of cylinderType by passing four parameters, debugger shows the height I want but, radius=x=y=0. So when I call method printVolume() on this object, it displays '0'.
Am I missing something important with inheritance?
Thank you~
#include <iostream>
using namespace std;
class circleType
{
public:
circleType();
circleType(double r);
double getArea() const;
private:
double radius;
};
class cylinderType : public circleType
{
public:
cylinderType(double h, double r);
void printVolume() const;
private:
double height;
};
int main()
{
cylinderType cylinderA(2, 4);
cylinderA.printVolume();
return 0;
};
circleType::circleType()
{
radius = 0;
};
circleType::circleType(double r)
{
radius = r;
};
double circleType::getArea() const
{
return (3.14 * radius* radius);
};
cylinderType::cylinderType(double h, double r)
{
circleType::circleType(r);
height = h;
};
void cylinderType::printVolume() const
{
cout << (circleType::getArea() * height);
};

C++ inheritance subclass modifiable variables

Help me understand how to use the superclass variable in the subclass and being able to make changes to it, I am making a clone Space Invaders 2D game using SDL library
First off I have a Rectangle class that is inherited from SDL_Rect looking like this i'll leave the unimportant parts out
//Rectangle.h
namespace galaxy{
struct Rectangle : public SDL_Rect{
Rectangle();
Rectangle(int xx, int yy, int hh, int ww);
Rectangel centeredRect(int width, int height) const
bool overlaps(const Rectangle& other) const;
};
}
I'll leave the .cpp out because it's fairly easy to see what part the rectangel is playing in this and I don't want to bore you guys out,
Then I have a Sprite class that is the superclass of the figures in the game,
namespace galaxy{
class Sprite{
public:
virtual void draw() = 0;
virtual ~Sprite();
virtual void keyLeft(SDLKey k);
virtual void keyRight(SDLKey k);
......more buttons
protected:
Sprite(int x, int y, int h, int w);
private:
Rectangle rect;
Sprite (const Sprite&);
const Sprite& operator=(const Sprite&);
};
}
In the .cpp file I have the following code
namespace galaxy{
Sprite::Sprite{int x, int y, int h , int w) : rect (x, y, h, w){}
Sprite::~Sprite(){}
const Rectangel& Sprite::getRect() const{
return rect;
}
void Sprite::keyLeft(SDLKey k){}
void Sprite::keyRight(SDLKey k){}
void Sprite::keyDown(SDLKey k){}
...more buttons
}
Then to where the problem is, I have another class Ship, where I want to overload the keyLeft from the superclass, having the rectangle rect following with coordinates, and I need to change the x, and y in the subclass, but when doing so with the construction I have below the r.x++; is behaving as inside a function and the changes to rectangle x is being cleared when exiting the function, when trying to reach rect in Ship class I get error not reachable, when getting rect through r = getRect(); the change to r is only inside the function but the ship does not move on screen.
//Ship.h
namespace galaxy {
class Ship : public Sprite{
public:
void keyLeft(SDLKey k);
void keyRight(SDLKey k);
void keyDown(SDLKey k);
void keyUp(SDLKey k);
void space(SDLKey k);
Ship(int x, int y, int hits);
private:
SDL_Surface* ship;
int hits;
};
}
//Ship.cpp
using namespace std;
namespace galaxy{
Rectangel r;
Ship::Ship(int x, int y, int hits) : Sprite(x, y, NULL, NULL), hits(hits){
ship = IMG_Load("Ship.png");
}
//Here is where my problem is.....
void Ship::keyLeft(SDLKey k){
std::cout << "Im here!\n";
std::cout << r.getX(); // outputs 250
r.x--;
std::cout << r.getX(); // outputs 251
}
void Ship::keyRight(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::keyDown(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::keyUp(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::space(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::draw(){
r = getRect();
SDL_BlitSurface(ship, NULL, sys.screen, &r);
}
}
right now Im doing this like:
#ifndef SHIP_H
#define SHIP_H
#include "Sprite.h"
#include <string>
namespace galaxy {
class Ship : public Sprite{
public:
/*static Ship* getInstance(int x, int y, int hits);*/
void draw();
//virtual void perform() = 0;
/*int getHits() const;*/
int getX() const;
int getY() const;
const Rectangel& getRect() const;
void keyLeft(SDLKey k);
void keyRight(SDLKey k);
void keyDown(SDLKey k);
void keyUp(SDLKey k);
void space(SDLKey k);
//~Ship();
//protected:
Ship(int x, int y, int hits);
private:
SDL_Surface* ship;
int hits;
Rectangel rect;
};
}
#endif
#include "Ship.h"
#include "Globals.h"
#include "Sprite.h"
#include <SDL_image.h>
#include <iostream>
using namespace std;
namespace galaxy{
Rectangel r;
/*Ship* Ship::getInstance(int x, int y, int hits){
return new Ship(x, y, hits);
}*/
Ship::Ship(int x, int y, int hits) : Sprite(x, y, NULL, NULL), hits(hits){
ship = IMG_Load("Ship.png");
}
const Rectangel& Ship::getRect() const{
return rect;
}
void Ship::keyLeft(SDLKey k){
std::cout << "Im here!\n";
rect.x--;
}
void Ship::keyRight(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::keyDown(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::keyUp(SDLKey k){
std::cout << "Im here!\n";
}
void Ship::space(SDLKey k){
std::cout << "Im here!\n";
}
/* int Ship::getHits() const{
return hits;
}*/
int Ship::getX() const{
return r.getX();
}
int Ship::getY() const{
return r.getY();
}
void Ship::draw(){
r = getRect();
SDL_BlitSurface(ship, NULL, sys.screen, &r);
}
}
But this is just a workaround so that I'm not stuck forever.
If you do something like this:
Rectangel r;
...
r = getRect();
Then you'll just get the original rectangle copied to your r variable, when you change it, the original Rectangel wont change.
Maybe for quick fix you could implement setRect function to your Ship class to be actually able to write back modified data.
void Ship::setRect(const Rectangel& rr) { rect = rr; }
And if you want to reach the rect from the Sprite base class directly then you need to give the rect protected visibility like so:
class Sprite
{
[...]
protected:
Rectangle rect;
[...]
};

C++ Composition program error(xcor undeclared )

Error!xcor undeclared at setAll() function
I had created object of PointType in Circle class to do sort of composition, but at the initialization of the constructor of Circle it is showing that
" [Error] 'ycor' was not declared in this scope "
" [Error] 'xcor' was not declared in this scope "
I want xcor and ycor in my Circle class in order to get the Radius using setAll() function
Please help!at what i am messing up.
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
class PointType{
private:
int xcor;//x coordinate
int ycor;//y coordinate
public:
PointType();//constructor
PointType(int x,int y);
void setPoint(int x,int y);
int getx() const;
int gety() const;
};
PointType::PointType():xcor(0),ycor(0)
{
}
PointType::PointType(int x,int y):xcor(x),ycor(y){
}
void PointType::setPoint(int x,int y){
xcor=x;
ycor=y;
}
int PointType::getx() const{
return xcor;
}
int PointType::gety() const{
return ycor;
}
class Circle{
protected:
float Radius;
float Area;
int Circumference;
float pi;
PointType obj1;
public:
Circle();
void setAll();
float getRadius();
float getArea();
float getCircumference();
void callFunction();
void printAll();
void pt(int x,int y);
};
Circle::Circle():Radius(0),Area(0),Circumference(0),pi(3.1415),obj1(xcor,ycor){
}
void Circle::setAll(){
Radius=sqrt( (xcor*xcor) + (ycor*ycor) );
Area=pi*Radius*Radius;
Circumference=2*pi*Radius;
}
float Circle::getRadius(){
return Radius;
}
float Circle::getArea(){
return Area;
}
float Circle::getCircumference(){
return Circumference;
}
void Circle::printAll(){
cout<<"The Area is :"<<Area<<endl;
cout<<"The Circumference is :"<<Circumference<<endl;
}
void Circle::pt(int x,int y){
obj1.setPoint(x,y);
}
Your class Circle doesn't have any member variable called xcor or ycor. If you want to get the values of your PointType object, your setAll function should look like:
Radius=sqrt( (obj1.getx()*obj1.getx()) + (obj1.gety()*obj1.gety()) );
Also you have to change your constructor:
Circle::Circle():Radius(0),Area(0),Circumference(0),pi(3.1415),obj1(0,0)
as it doesn't have access to any xcor or ycor either.
You have to change your Circle constructor, because it knows nothing about xcor and ycor:
Circle::Circle():Radius(0),Area(0),Circumference(0),pi(3.1415),obj1(42,56)
The Circle class still doesn't know about xcor and ycor, so you have to change setAll method:
Radius=sqrt( static_cast<double>((obj1.getx()*obj1.getx()) + (obj1.gety()*obj1.gety())) );
declare xcor,ycor to Circle' constructor argument, and
add this line before computing Radius:
int xcor = obj1.getx(), ycor = obj1.gety();
Radius=sqrt( (xcor*xcor) + (ycor*ycor) );
You could also consider to inherit PointType for Circle, instead of embedded obj1:
class Circle : public PointType
{
public: Circle(int x, int y):PointType(x, y) {}
...
/* remove obj1 */
}