How to use members from other files in main c++ code file? - c++

I want to know how to use members from other files in main c++ code file?
I already know that we can put our declarations in header files.
Let me summarize my current knowledge and missing parts:
Example 1 - using a function from another file
other.cpp
int Add(int a,int b)
{
return a+b;
}
main.cpp
int Add(int,int); //the most important part
int main()
{
Add(12,2);
}
Example 2 - using a global variable from another file
other.cpp
int something=12;
main.cpp
extern int something; //the most important part
int main()
{
cout << something;
}
Question 1 - create an object from a class in another file???
other.cpp
class Rectangle
{
public:
int width;
int height;
int area() { return height*width }
};
main.cpp
int main()
{
Rectangle a; //gives me error,what should i do?
}

You should start using header files. Typically you would have
Rectangle.h a header file with the declarations
class Rectangle{
public:
Rectangle();
Rectangle(int x, int y);
int width;
int height;
int area();
};
Rectangle.cpp Then you'd have a corresponding cpp file with the definitions
#include "Rectangle.h"
Rectangle::Rectangle()
{
width = 0;
height = 0;
}
Rectangle::Rectangle(int x, int y)
{
width = x;
height = y;
}
Rectangle::area()
{
return height*width;
}
Now in your main.cpp
#include "Rectangle.h"
int main()
{
Rectangle a;
}

You need to declare a class Rectangle in main.cpp, preferably using a header file, ex:
other.h
class Rectangle
{ public:int width; int height; int area(); };
other.cpp
#include other.h
int Rectangle::area() { return height*width; }
main.cpp
#include "other.h"
int main()
{
Rectangle a; //gives me error,what should i do?
}

Following your pattern, it should be simply class Rectangle; (forward declaration).
However, you really should consider to create a Rectangle.h and Rectangle.[C|cpp|cc] (any of them), and include the header instead of forward declaring.
The header should contain the declaration, the source the definition.

Place the declaration of the class in a header file. The definition is usually placed in a source file which uses #include to reference the declaration header. Any other source file which require usage of the class will then simply #include the class declaration header.

Related

How to create a function inside a structure in a header file and implement this function in another c source file?

Here I created a main.c
#include <iostream>
#include "Header.h"
struct person_t a;
int main()
{
a.func(1,2);
}
And I have a header called "Header.h", I define a structure in this file
struct person_t {
char name;
unsigned age;
int(*func)(int, int);
};
Then I implement int(*func)(int, int); in another c file called "Source.c"
#include "Header1.h"
struct person_t a;
int add2(int x, int y)
{
return x + y;
};
a.func = &add2;
But it seems not work, does anyone who has any idea of this question?
You may not use statements outside a function like this
a.func = &add2;
Remove this statement and this declaration
struct person_t a;
from Source.c.
In the header place the function declaration
int add2(int x, int y);
And in the file with main write
int main()
{
struct person_t a;
a.func = add2;
a.func(1,2);
}

How to get area of rectangle using class?

I have been tasked to calculate the area and perimeter of a rectangle using class. The input function is supposed to be inside class. I wrote code so far but there seem to be errors I can't detect.
Some help would be appreciated.
rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle{
public:
int width, length;
Rectangle();
Rectangle(int width1,int length1);
void getRectangle();
int getArea();
int getPerimeter();
};
#endif // RECTANGLE_H
rectangle.cpp
// oporer ta class
#include<iostream>
#include "rectangle.h"
Rectangle::Rectangle()
{
width=0;
length=0;
}
Rectangle::Rectangle(int width1,int length1)
{
width=width1;
length=length1;
}
void Rectangle::getRectangle()
{
cin>>width>>length;
}
int Rectangle::getArea()
{
return (width*length);
}
int Rectangle::getPerimeter()
{
return (width+length)*2
}
// oporer ta rectangle cpp
main.cpp
#include <iostream>
#include "rectangle.h"
using namespace std;
int main()
{
Rectangle paraFirst();
paraFirst.getRectangle();
return 0;
}
// main fucntion
Assuming that the #includes work in your local setup, I see two typos here:
cin>>width>>length; must std::cin >> width >> length;
missing semicolon after return (width+length)*2
And probably the main problem:
Rectangle paraFirst();
is parsed as a declaration for a function that takes no arguments and returns a Rectangle. See also Most vexing parsing. To call the default constructor simply use
Rectangle paraFirst;
or
Rectangle paraFirst{};

multiple definition, already defined here

I have three simple files.
”banana.cc“
namespace ocr{
int a = 5;
}
"apple.cc"
#include "banana.cc"
namespace ocr{
int b = a;
}
"main.cc"
#include "apple.cc"
int main()
{
return 0;
}
/tmp/ccs6XmP2.o:(.data+0x0): multiple definition of `ocr::a'
/tmp/ccEkxDgJ.o:(.data+0x0): first defined here
/tmp/ccs6XmP2.o:(.bss+0x0): multiple definition of `ocr::b'
/tmp/ccEkxDgJ.o:(.bss+0x0): first defined here
/tmp/cco0dUCm.o:(.data+0x0): multiple definition of `ocr::a'
/tmp/ccEkxDgJ.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
after compiler insert all the #include, main.cc is like:
namespace ocr{
int a = 5;
}
namespace ocr{
int b = a;
}
int main()
{
return 0;
}
why this will cause redefinition?
Thank you.
Because you're compiling apple.cc and banana.cc and main.cc in your project.
So you're compiling this file:
namespace ocr{
int a = 5;
}
and this file:
namespace ocr{
int a = 5;
}
namespace ocr{
int b = a;
}
and this file:
namespace ocr{
int a = 5;
}
namespace ocr{
int b = a;
}
int main()
{
return 0;
}
Obviously ocr::a is defined in all three files, and ocr::b is defined in two of them.
Don't have enough reputation to comment but I just want to elaborate on this, just in case there is still confusion.
If you want to share some variables between files, create a header file and declare it there.
i.e.
// common.h
namespace ocr{ int a, b; }
// banana.cc
#include "common.h"
void initAppple(){
ocr::a = 4;
}
// apple.cc
#include "common.h
void initBanana(){
ocr::b = a;
}
// main.cc
#include "common.h"
int main(){ initApple(); initBanana(); }
And then when you compile main.cc, link it with apple.cc and banana.cc instead of "including" it.
g++ main.cc apple.cc banana.cc -o output
Note that you can't just declare and initialize separately in global scope, which is why you probably need to use a setter function such as the ones above (initApple() etc). Or use extern inside the header file and define it inside the source file.

C++ Inheritances - Files

Im trying to seperate a class and its superclass into two different header and cpp files. In the main Method I want to include both of them.
Currently my main programm example.cpp looks like this:
#include <iostream>
#include "header.h"
using namespace std;
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
My Rectangle.cpp like this:
#include <iostream>
#include "Shape.h"
#include "Rectangle.h"
using namespace std;
int getArea()
{
return (Shape::width * Shape::height);
}
And my Rectangle.h:
class Rectangle: public Shape
{
public:
int getArea();
};
The Shape.cpp:
#include <iostream>
#include "Shape.h"
using namespace std;
void Shape::setWidth(int w)
{
width = w;
}
void Shape::setHeight(int h)
{
height = h;
}
And the Shape.h:
class Shape
{
public:
void setWidth(int w);
void setHeight(int h);
int width;
int height;
};
The header.h just includes the two headers of the classes:
#include "Shape.h"
#include "Rectangle.h"
If I compile it, the compiler says:
Lukass-MacBook-Pro:Oberklassenbeispiel Lukas$ g++ -c Rectangle.cpp
Rectangle.cpp:9:17: error: invalid use of non-static data member 'width'
return (Shape::width * Shape::height);
~~~~~~~^~~~~
Rectangle.cpp:9:32: error: invalid use of non-static data member 'height'
return (Shape::width * Shape::height);
~~~~~~~^~~~~~
2 errors generated.
It seems, that the Rectanlge.cpp cant see the attributes of the superclass. How do I fix this?
When you #include, you provide the file name, not the classes specified in the file. It is quite common to have multiple closely coupled classes in a single file. Your example isn't really the best (it is single inheritance, not multiple as the title suggests), but working with that...
You could have a file, shapes.h with the following contents :
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
Then in your main function (and the file containing it).
#include <iostream>
#include "shapes.h"
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}

Error While Creating Header Files in C++

There is one header a file Rectangle.hxx
#ifndef Rectangle_included
#define Rectangle_included
#include <iostream>
#include <math.h>
#include "GetL.hxx"
using namespace std;
class Rectangle: public GetL
{
int width;
int value;
public:
Rectangle();
Rectangle(int v, int w);
Rectangle(const Rectangle& b);
int getWidth();
int getValue();
Rectangle & plus(int newval);
};
#endif //Rectangle_included
The file GetL.hxx is defined like this:
#ifndef GetL_included
#define GetL_included
#include <iostream>
using namespace std;
class GetL
{
public:
virtual int getWidth();
};
#endif //GetL_include
The file Rectangle.cxx contains various definitions:
#include <iostream>
#include <math.h>
#include "Rectangle.hxx"
using namespace std;
Rectangle::Rectangle()
{
value=0;
width=0;
}
Rectangle::Rectangle(int v, int w)
{
value=v;
width=w;
}
Rectangle::Rectangle(const Rectangle& b)
{
value= b.value;
width= b.width;
}
int Rectangle::getWidth()
{
return width;
}
int Rectangle::getValue()
{
return value;
}
Rectangle& Rectangle::plus(int newval)
{
value+=newval;
if(value>=pow(2,width))
cout<<"Overflow";
return *this;
}
But i am getting the error on compiling Rectangle.cxx.
/tmp/cclETn3R.o:Rectangle.cxx:(.text$_ZN4GetLC2Ev[GetL::GetL()]+0*8): undefined reference to 'vtable for Getl'
How can i remove it? How can i define file GetL.cxx or i don't need to?
You need to compile the different files without linking first. On UNIX compilers this is typically done using the -c option. When building the executable you then specify all the produced .o objects. Alternatively you can specify all source files at once but this is really only viable for very small projects.
You must implement GetL::getWidth(). Given your other files, you will probably implement it in GetL.cxx.