So I'm a begging in C++, Im making a program to calculate the area and the perimeter of a triangle when the user inputs the base, height and sides. I have 2 classes, Area and Perimeter, I need to access the variable "base" from Area and use them in Perimeter, since I need the base to calculate perimeter. How can I do this? Also would it be more effective to use one class for this program?
Area.h
#ifndef AREA_H
#define AREA_H
#include <iostream>
using namespace std;
class Area
{
private:
int height;
public:
int base;
Area();
int calcArea();
};
#endif // AREA_H
Area.cpp
#include "Area.h"
#include <iostream>
using namespace std;
Area::Area()
{
cin >> base;
cin >> height;
};
int Area::calcArea(){
int answer;
answer = (base * height)/2;
return answer;
}
Perimeter.h
#ifndef PERIMETER_H
#define PERIMETER_H
#include "Area.h"
#include <iostream>
using namespace std;
class Perimeter
{
private:
int s1;
int s2;
public:
Perimeter();
int calcP();
};
#endif // PERIMETER_H
Perimeter.cpp
#include "Perimeter.h"
#include "Area.h"
#include <iostream>
using namespace std;
Perimeter::Perimeter()
{
cin >> s1;
cin >> s2;
}
int Perimeter:: calcP(){
int answer;
answer = s1 + s2 + base ;
return answer;
}
Instead of creating two classes, Create a class Triangle.
class Triangle
{
private:
int base;
int height;
public:
Triangle();
int CalculatePerimeter();
int CalculateArea();
};
Define the functions.
Create objects of Triangle and call the functions.
Related
I am creating a C++ project and it's a console application and I have to show premiere league table with using structs , arrays ,file stream and input and output I made a struct but I don't have and idea what to do next and it has to be on console not GUI. I tried making a struct but don't know what to do
#include <iostream>
using namespace std;
#include <string>
struct standing
{
int position;
char name[20];
int matches;
int win;
int draw;
int lose;
int GD;
int points;
};
#include <string>
#include <vector>
struct Team
{
private:
int rank;
std::string name;
int matches;
int win;
int draw;
int lose;
int GD;
int points;
};
struct League
{
private:
std::vector<team> teams;
}
Say we have these two structs. One struct for a team the other one for the whole league.
We can now add some constructors for Team struct (trivial) and League struct and public methods.
struct League
{
public:
void addTeam(Team team_)
{
teams.push_back(team_);
};
Team getChampion()
{
// simple algo to get winning team
}
std::pair<Team, Team> getRegulated(){
// simple algo to get regulated teams
};
private:
std::vector<team> teams;
};
https://www.cplusplus.com/doc/tutorial/structures/
So I am using Visual Studio to make a simple program for intro to Object Oriented Programming. I am using C++ language to do this, in Netbeans with JAVA OPP isn't so complicated but I am having trouble here. I have to make a simple Object, I chose to make my object called Movie. I made a Movie.h and Movie.cpp file. I included .h's extensions to my Movie.cpp and main.cpp but when I create the object in my main or try to i keep getting errors and underlines because my .cpp file is not recognizing my variables declared in .h, it keeps saying the variable is undefined.
So visual studio's wants to help me out and I followed their method so in my .h files they said they will define my methods for me, meaning they will set it up for me and when I clicked it i get this format
string Movie::getName()
{
return string();
}
While I am using this format for this function
string getName(){
return name;
}
My variables keeps getting red underlined saying they are undefined.
My Movie.h file
#pragma once
//Header File is where all of your class defenitions will go.
class Movie
{
private:
string name;
int length;
double rating;
public:
//Constructors
//A Default constructor
Movie();
//A Constructor that takes in 3 values, an int, a double and a string
Movie(int x, double y, string z );
//Get fucntions
//Get fucntions will get the required values.
string getName();
int getLength();
double getRating();
//Set functions
//Set fucntions will set the variables to the input values.
void setName(string x);
void setLength(int y);
void setRating(double z);
//toString Function
//A toString function that will display the details of the object
void toString();
};
My Movie.cpp File
#include "Movie.h"
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//Default Construtor
Movie::Movie()
{
}
//A Construtor that takes in 3 values, int for length, double for rating, string for name
Movie::Movie(int x, double y, string z)
{
setLength(x);
setRating(y);
setName(z);
}
//Set functions
void setLength(int x) {
length = x;
}
void setRating(double y) {
rating = y;
}
void setName(string z) {
name = z;
}
//Get functions
int getLength() {
return length;
}
double getRating() {
return rating;
}
string getName() {
return name;
}
My .main
#include "pch.h"
#include <iostream>
#include "Movie.h"
#include <string>
using namespace std;
int main()
{
Movie mo1();
mo1.setName("Inceptio");
mo1.setLength(123);
cin.ignore(1);
return 0;
}
Now I havent worked on my toString method yet but I can't because I cant figure out what the real problem is, why is my .cpp not recognizing my variables? Is my format wrong? Is the visual studio's format right because I ran with their format and got bunch of errors as well or am I declaring them wrong or something? Thank you!
Main.cpp
//#include "pch.h"
#include <iostream>
#include "Movie.h"
#include <string>
using namespace std;
int main()
{
Movie mo1;
mo1.setName("Inceptio");
mo1.setLength(123);
cin.ignore(1);
return 0;
}
Movie.h
#pragma once
#ifndef Movie_H
#define Movie_H
#include <string>
#include <iostream>
using namespace std;
//Header File is where all of your class defenitions will go.
class Movie
{
private:
string name;
int length;
double rating;
public:
//Constructors
//A Default constructor
Movie();
//A Constructor that takes in 3 values, an int, a double and a string
Movie(int x, double y, string z);
//Get fucntions
//Get fucntions will get the required values.
string getName();
int getLength();
double getRating();
//Set functions
//Set fucntions will set the variables to the input values.
void setName(string x);
void setLength(int y);
void setRating(double z);
//toString Function
//A toString function that will display the details of the object
void toString();
};
#endif
Movie.cpp
#include "Movie.h"
//#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//Default Construtor
Movie::Movie()
{
}
//A Construtor that takes in 3 values, int for length, double for rating, string for name
Movie::Movie(int x, double y, string z)
{
setLength(x);
setRating(y);
setName(z);
}
//Set functions
void Movie::setLength(int x) {
length = x;
}
void Movie::setRating(double y) {
rating = y;
}
void Movie::setName(string z) {
name = z;
}
//Get functions
int Movie::getLength() {
return length;
}
double Movie::getRating() {
return rating;
}
string Movie::getName() {
return name;
}
I am new to c++ and is trying to learn about its Object-Oriented design. I started a small project to test out inheritance and polymorphism but encountered a problem and can't seen to figure out what went wrong.
Whenever I compile, there will be an error "class 'ShapeTwoD' has no member name getx() and gety()". I tried to directly set the x and y value with setx and sety but it still return the same error.
Class ShapeTwoD is the Base class with only the variable 'name' and 'container'. Would appreciate if anyone can direct me to the right direction.
Main.cpp
#include <iostream>
#include <string>
#include "ShapeTwoD.h"
#include "Square.h"
using namespace std;
int main()
{
cout<<endl;
ShapeTwoD *shape2D[100];
ShapeTwoD *sq1 = new Square("Square", true, 4, 6);
cout << sq1->getName() <<endl;
cout << sq1->getContainer() <<endl;
//sq1->setx(4) <<endl;
//sq1->sety(6) <<endl;
cout << sq1->getx() <<endl;
cout << sq1->gety() <<endl;
cout<<endl;
delete sq1;
}
Square.h
#include <iostream>
#include <string>
#include "ShapeTwoD.h"
using namespace std;
class ShapeTwoD; //forward declare
class Square : public ShapeTwoD
{
public:
int x;
int y;
//constructor
Square(string name, bool container,int x, int y);
int getx();
int gety();
void setx(int x);
void sety(int y);
};
Square.cpp
#include <iostream>
#include <string>
#include "Square.h"
#include "ShapeTwoD.h"
Square::Square(string name, bool containsWarpSpace, int coordx, int coordy)
:ShapeTwoD(name, containsWarpSpace)
{
(*this).x = coordx;
(*this).y = coordy;
}
int Square::getx()
{
return (*this).x;
}
int Square::gety()
{
return (*this).y;
}
void Square::setx(int value)
{
(*this).x = value;
}
void Square::sety(int value)
{
(*this).y = value;
}
That's normal... If you declare sq1 as ShapeTwoD, you have access to ShapeTwoD public member methods/attributes. Even it was instanciated with the Square constructor. Cast it as Square, and you can use getx gety. Or declare getx/gety as methods of ShapeTwoD.
Well this is what you should expect since it has shape2D type , Although constructing it with the square constructor won't allow you to access the derived class members but it will allow you to have a safe type cast to use it.. the simplest way to do it is by :
cout << static_cast<Square*>(sq1)->getx() << endl;
cout << static_cast<Square*>(sq1)->gety() << endl;
I have a C++ project in Visual Studio 2015.
GameManager.h and Input.h both give me a syntax error: identifier 'Player'. This happens because I want to give an object of type 'Player' as an argument to functions in these two Header files and their appropriate C++ Files.
How do I fix that? For further information I have provided my code.
main.cpp:
#include "GameManager.h"
#include "Input.h"
#include "Player"
#include <iostream>
#include <string>
using namespace std;
const int maxPlayerCnt = 10;
static Player p1, p2, morePlayers[maxPlayerCnt];
int main()
{
GameManager game;
game.Game(p1, p2, morePlayers);
return 0;
}
It creates an object of type GameManager and 3 objects of type Player.
GameManager.h:
#include "Player.h"
class GameManager
{
public:
void Game(Player p1, Player p2, Player morePlayers[]);
};
GameManager.cpp:
#include "GameManager.h"
void GameManager::Game(Player p1, Player p2, Player morePlayers[])
{
int playerCnt = 0;
Input input;
input.getPlayerDetails(playerCnt, p2);
input.getMorePlayerDetails(playerCnt, morePlayers);
}
It creates an object of type Input to use further functions and will get more code, once I figure this problem out. And then calls to functions with specific arguments it gets from main.cpp
Input.h:
#pragma once
#include "Player.h"
#include <iostream>
#include <string>
using namespace std;
class Input
{
public:
Input();
void getPlayerDetails(int &playerNum, Player p);
void getMorePlayerDetails(int &playerNum, Player p[]);
};
It includes everything Input.cpp needs and initializes the funcitons
Input.cpp:
#include "Input.h"
void Input::getPlayerDetails(int &playerNum, Player p)
{
playerNum++;
string currentName;
char currentSymbol;
cout << "Player " << playerNum << ", what is your name?\n";
cin >> currentName;
p.setName(currentName);
cout << currentName << " what is your symbol?\n";
cin >> currentSymbol;
p.setSymbol(currentSymbol);
}
void Input::getMorePlayerDetails(int &playerNum, Player p[])
{
int plNum = playerNum;
if (playerNum >= 12)
cout << "You can't get another player!\n";
else
{
//getPlayerDetails(p[playerNum - 2], (plNum - 2));
}
}
It for now has all the functions needed and both get an object of type Player. And the second function is not quite done now. But that is not important.
Player.h:
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Player
{
private:
string _name;
char _symbol;
public:
Player();
void getName();
void setName(string name);
void setSymbol(char symbol);
};
Player.cpp:
#include "Player.h"
Player::Player()
{
}
void Player::getName()
{
cout << "I have no name!\n";
}
void Player::setName(string name)
{
_name = name;
}
void Player::setSymbol(char symbol)
{
_symbol = symbol;
}
If you can help me, I would be pleased to see your response.
So here is the main where i'm trying to call the array by a pointer:
#include <iostream>
#include "Lottery.h"
#include "Player.h"
#include "LotteryData.h"
using namespace std;
int main()
{
Player player;
Lottery random;
LotteryData data;
player.Input();
random.setRandomNumber();
data.PassInfo(int (&Numbers)[6][6]);
}
Apparently "Numbers" is undefined even though the header is included, here's the header and .cpp files relating to it.
LotteryData.h
#pragma once
#include <iostream>
#include <fstream>
#include "Lottery.h"
#include "Player.h"
using namespace std;
class LotteryData
{
private:
public:
LotteryData();
~LotteryData();
void PassInfo(int (&Numbers)[6][6]);
};
LotteryData.cpp
#include <iostream>
#include <fstream>
#include "LotteryData.h"
using namespace std;
LotteryData::LotteryData()
{
}
LotteryData::~LotteryData()
{
}
void LotteryData::PassInfo(int (&Numbers)[6][6])
{
int* ptr;
FILE *Numfile;
Numfile = fopen("C:/Num.txt", "wb");
ptr = &Numbers[6][6];
for (int i=0; i<36; i++)
{
fwrite(ptr, sizeof(int), 36*36, Numfile);
}
fclose(Numfile);
//ofstream out("Numbers.txt");
}
Everything seems fine, I'm puzzled why the reference in the main says the array is undefined, any ideas?
edit: Apologies, missed some bits
Player.h
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
class Player
{
private:
public:
Player();
~Player();
void Input();
int Numbers[6][6];
};
Player.cpp
#include <iostream>
#include <fstream>
#include "Player.h"
using namespace std;
Player::Player()
{
}
Player::~Player()
{
}
void Player::Input()
{
int num(0);
int duplicate = 0;
int game = 0;
int NumberofGames = 0;
cout<<"How many games do you want to play for this weeks draw?"<<endl;
cin>>NumberofGames;
if (NumberofGames>6)
{
cout<<"Please enter an amount between 1 and 6"<<endl;
cin>>NumberofGames;
}
do
{
for (int i=0;i<6;i++)
{
cout<<"Enter Number "<< (i+1) <<endl;
cin>>num;
if (num > 0 && num <67)
{
Numbers[game][i]= num;
}
else
{
cout <<"Please enter number between 1 and 66"<<endl;
i = i-1;
}
}
game = game + 1;
NumberofGames = NumberofGames - 1;
}
while (NumberofGames=0);
}
void PassInfo(int (&Numbers)[6][6]);
That line does not declare an array - it declares a function. You have no declaration for an array in your class (in fact, you have no data members declared in your class at all).
If you want to declare a member array, you need to modify your class definition:
class LotteryData
{
private:
int Numbers[6][6]; // this declares an array
public:
LotteryData();
~LotteryData();
void PassInfo(int (&arr)[6][6]); // this is still a function declaration
};
Just because you made a function's parameter be named Numbers doesn't magically mean that your program has an array called Numbers declared in it.
So a Player has an array called "Numbers".
Then you would use it like this:
data.PassInfo(player.Numbers);
In your main() function,
data.PassInfo(int (&Numbers)[6][6]);
This is wrong. You should simply pass a reference to 2D array.
int (&Numbers)[6][6];
data.PassInfo(Numbers);