Visual C++ :: Triangle Class With Member Variables

Apr 8, 2015

C++
Create a Triangle class that has the following member variables:
side1 - a double
side2 - a double
side 3 - a double perimeter area

The class should have the following member functions:

- default constructor that sets the value of all 3 sides of a triangle to 0.0

- a constructor with arguments that accepts values for the three sides of a triangle (member variables) as arguments

- setDimensions - a function that allows the value of the three sides to be entered by the user through the keyboard

- testSides - a function that determines if the 3 values entered can actually be the sides of a triangle. If they do not create a triangle, print the values entered and an appropriate message
--The sum of any two side lengths of a triangle must always be greater than the length of the third side: so side 1 + side 2 > side 3 and side 1 + side 3 > side 2 and side 2 + side 3 > side 1 ( all three must be true for the 3 values to make a triangle)

- getSide1 - a function that returns the value of side 1, getSide2 - a function that returns the value of side 2, getSide3 - a function that returns the value of side 3

- getArea - a function that returns the area of a triangle: The formula for the area of a triangle (when the height is not known) is: A = sqrt (p(p-side1)(p-side2)(p-side3)) where p = (side1+side2+side3)/2

- getPerimeter - a function that returns the perimeter of a triangle: Perimeter = side1 + side2+ Side 3

- A displayTriangleInfo function that displays side1, side2, side3, area, and perimeter for a triangle object.

After testing your code for a single object, create an array of 5 triangles. Use a for loop and the setDimensions function to allow the user to set the values for the 3 sides of a triangle, test the vales entered to determine if the 3 create a triangle. If they do create a triangle than use the getArea and getPerimeter functions to calculate the Area and Perimeter for the triangle and use the displayTriangleInfo function to display all of the data for that triangle. If the three values do not create a triangle then print the 3 numbers entered and an appropriate message. In either case the loop should then move on and get the data for the next triangle from the user.

View 7 Replies


ADVERTISEMENT

C++ :: Class Member Variables Run Out Of Scope?

Mar 21, 2012

Code:

class CObjects {
int m_CurrentTime;
int m_Steps;
AStarList* OPEN;
AStarList* CLOSED;
std::vector<AStarNode *>solution;

[code]....

CCB is derived from CrowdEntity and in turn is derived from CObjects Inside CObjects, I declared AStarList *OPEN; Why would howmany become garbage (cdcdcdcd) when I reference it in GetBestNode()

View 9 Replies View Related

C++ :: Memory Address Of Class Member Variables?

Jun 22, 2013

Suppose I have two classes, MyClassX and MyClassY, each with two member variables, as defined below. I create an object instance of each class, and then create a pointer to each member variable for each object:

Code:
class MyClassX
{
public:
int a;
double b;
MyClassX(int _a, double _b)

[code]....

After converting the hexadecimal to decimal, it appears that with MyClassX, pxb is 8 bytes from pxa, whereas for MyClassY, pya is only 4 bytes from pyb. This makes sense for MyClassY, because the first member variable to be stored is an int, and so will occupy 4 bytes. However, why should this be any different for MyClassX, which also has an int as the first member variable, so shouldn't this also occupy 4bytes?

The reason I have come across this problem is that I am looking into streaming objects to memory and then loading them again. (I know boost can do this, but I am trying it out myself from scratch.) Therefore, this is causing an issue, because I cannot just assume that the size of memory occupied by an object is just the sum of the sizes of its member variables. MyClassX is 8 bytes larger than MyClassY, even though the intuition is that it should only occupy 4 bytes more due to a double being replaced by an int.

View 4 Replies View Related

C++ :: Accessing Classes Member Variables Nested Inside Another Class

Feb 22, 2013

I have two classes, a Package class and a Person class. The Package class has two Person objects has member variables, a Sender and a Receiver. While overloading the << operator for the Package class so that it will make an output label from everything in the Package class. Here is my code...

class Package{
public:
Person Sender;
Person Reciever;
int weight;
double cost;
friend ostream &operator<<(ostream &out, Package &pack);

[Code] .....

So my problem is on that last output line, I am unable to call Sender.getName()... etc. Is there a proper syntax so that I can access the members of the Person class while overloading the << operator for the Package class?

View 2 Replies View Related

Visual C++ :: Declaring Member Variables In Windows Form App

Sep 30, 2014

I have created a windows form project in visual studio so that I can use a windows form to interact with the game class that I'm creating, but I'm running into some problems.

For one thing, I would like to be able to call Image::FromFile() one time only during initialization, and store the result in a member variable (called mBGImage). From what I can tell, the variable needs to be of type String^ (this caret symbol is new to me, but I understand it is the "managed code" version of the standard pointer, which would look like String*).

When I try to compile the following code (located in my header file) I get the error :

"error C3265: cannot declare a managed 'mBGImage' in an unmanaged 'BSG::BSGame'".

How can I store the result of Image::FromFile() permanently in my class?

When I try to declare a global variable of type "Image^", I get "error C3145: global or static variable may not have managed type System:rawing::Image ^"

#include "stdafx.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;

[Code] ......

View 2 Replies View Related

Visual C++ :: Exporting Class Or Member Function?

Oct 2, 2012

Usually we use the following statements to export a class or member functions,

Code:
#ifdef DLLMICRO
#define DLLIO __declspec(dllexport)
#else
#define DLLIO __declspec(dllimport)
#endif

I understand that the files using the exported class or function need to call this class or function with dllimport and the file containing the exported class or function needs to call this class or function with dllexport. But I tried to use __declspec(dllexport) only instead of the statements above. It still works. Is there anything I am missing?Why'd we have to switch between dllexport and dllimport?

View 12 Replies View Related

Visual C++ :: FileSet - Cannot Access Variables From Document Class

Jul 31, 2013

I have written a class "FileSet " in program's Document class.

This class contains other Four classes as

"Machine1" , "Machine2", "Machine 3" and "Machine 4".

Now my problem is Machine 1 can not access variables from Document class or other variables from FileSet.

How to access them ?

View 9 Replies View Related

Visual C++ :: Static Class Member In Multithreaded Environment

Nov 21, 2014

I have a class having static member.I have get and set methods which will Get and Set Values to this variable. In a multithreaded application does it have any thread safety issues.

Class a {
static int b;
void Set (int c);
int Get();
};

View 10 Replies View Related

C++ :: How To Access Member Variables From Main

Nov 10, 2013

my clsLocalStudent inherits from clsStudent. How to i set my accounts information (accountNumber, accountHolderID . . . . .)?

Code:
#include <iostream>
#include "clsInterest.h"
#include "clsDate.h"

[Code]......

View 3 Replies View Related

C++ :: Member Variables On Stack Or Free Store?

Feb 18, 2014

I'm struggling with the whole stack/heap thing. I totally see the need to create objects on the free store to outlive functions, however, I don't see the purpose of "new" when working with classes.

Code:
class Person{
public:
Person(std::string strName, int number);
~Person(void);

Resource r;

[Code] .....

Now, what is the difference? Why would I need to create a Resource on the free store? Both variables named r live until the destructor runs, right?

View 1 Replies View Related

C++ :: Void Triangle - Print Class Function

Sep 1, 2013

//Point.cpp
#include "Point.h"
#include <iostream>
#include <cmath>
using namespace std;

Point::Point() { //Initialise the point to the origin.

[Code] ....

void Triangle::print() { //print out the Triangle with the format "( (x1, y1), (x2, y2), (x3, y3) )"

How do I accomplish this? When i test with cout << _point1.print(), there's an error:

[Error] no match for 'operator<<' in 'std::cout << ((Triangle*)this)->Triangle::_point1.Point::print()'

View 4 Replies View Related

C++ :: Program That Will Represent Axis-aligned Right Triangle In X-y Plane As A Class

Apr 10, 2013

I am having trouble of exactly how "class" works. I dont know what the difference between set and get is. I have this code:

#include <iostream>
#include <cmath>
using namespace std;
class Point {
private:
double px;
double py;

[Code] .....

How to get void Triangle::setBottomLeftX(const double x) to work.

Implement the get and set member functions for the class Triangle. Use the appropriate class attributes of the class Triangle.

a. The location of the bottom left vertex is stored in the member attribute blPoint.
b. The top left vertex can be computed from blPoint and the height.
c. The bottom right vertex can be computed from blPoint and the length.

View 19 Replies View Related

C++ :: How To Initialize Static Member Of Class With Template And Type Of Nested Class

Oct 7, 2014

How to initialize a static member of a class with template, which type is related to a nested class?

This code works (without nested class):

#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>

[Code] ,....

View 1 Replies View Related

C++ :: Using Member Function Of A Class In Another Class And Relate It To Object In Int Main

Aug 21, 2013

I am writing a program which is using SDL library. I have two different classes which one of them is Timer Class and the other is EventHandling Class.

I need to use some member functions and variables of Timer in some Eventhandling Class member functions, Although I want to define an object of Timer in int main {} and relate it to its member function that has been used in Eventhandling member function in order that it becomes easier to handle it, I mean that I want to have for example two objects of timer and two objects of Eventhandling class for two different users.

I do not know how to relate an object of a class from int main{} to its member function which is being used in another class member function.

Lets have it as a sample code:

class Timer {
private:
int x;

public:
Timer();
get_X();
start_X();

[Code] ....

View 4 Replies View Related

C++ :: Cannot Access Private Member Declared In One Class But Can In Another Class

Sep 4, 2014

So I have an ImageManager class, Board class, and Box class. In Board.h I can declare ImageManager imgr; and in Board's constructor I can use imgr and its functions and such. However, in Box.h when I try and declare ImageManager imgr; I get the error "cannot access member declared in class ImageManager". Both declarations are under private, and exactly the same, but one doesn't work. Also, is there a way to only have one instance of ImageManager?

View 19 Replies View Related

Visual C++ :: Access Member Function From Non-member Function In Same CPP File?

Dec 16, 2012

In my MFC, CMyPorpertyPageDlg is derived from CPropertyPage. How to access its member function from a nonmember function in the same CPP file?.

void Non_Member_Get_PorpertyPage()
{
CMyPorpertyPageDlg* pPageDlg = ....
}

View 4 Replies View Related

C++ :: Class Has No Member?

Jun 26, 2014

I am having compiling issues and am looking for an explanation as to what is causing the error and how to fix it. The declaration of 'g4vuplInstanceID' seems to be global in scope in my option, however I may be wrong.

Compiler Error: error: 'class myPhysListGeneral' has no member named 'g4vupInstanceID'

#include "G4VUserPhysicsList.hh"
#include "G4VUPLSplitter.hh"
#include "G4VPhysicsConstructor.hh"
#include "myPhysListGeneral.hh"
#include "G4ParticleDefinition.hh"
#include "G4ProcessManager.hh"
#include "G4ParticleTable.hh"

[code]....

View 19 Replies View Related

C++ :: Initializing Variables In Each Class

Sep 26, 2014

I am working on a homework assignment and have most of the program working, but when I try to compile it keeps telling me to initialize the coin variables in each class. However, they are supposed to be added then removed so I don't want to set them back to zero.

Rewrite the Purse program given in Page 35 with functions to perform insert and remove operations. The function insert (int p, int n, int d, int q) will initialize pennies, nickels, dimes and quarters. The function dollars() will return the dollars. The function remove (int p, int n, int d, int q) will subtract pennies, nickels, dimes and quarters. The function display() returns a new String to print the content of the purse with remaining pennies, nickels, dimes and quarters.

Code:
usingnamespace std;
int insert_money (int *p, int *n, int *d, int *q);
int remove_money (int *p, int *n, int *d, int *q);
int dollars();
int main()

[Code] ....

View 2 Replies View Related

C++ :: Accessing Variables Of Another Class?

Aug 3, 2013

I've been attempting to create a game with curses and I keep running into the problem of scope. I want to change or use variables of a different class in a different header file. But I don't know if I should use pointers, references or neither. Should I be programming in a manner that doesn't make it necessary to use variables outside of their class?

View 1 Replies View Related

C++ :: Member Function In Derived Class Call Same Function From Its Base Class?

Sep 18, 2013

How can a member function in my derived class call the same function from its base class?

View 1 Replies View Related

C++ :: Using Non-function Member Into A Class

Oct 21, 2013

I mount a function (parameter - numeric vector; returns a string). However, this same function is used in several classes. To avoid that I keep duplicating the same code within these classes there is a way to do that as the code below?

std::string func( const vector<int> vec ) {
//processamento
return result;
} class A {

[Code] ....

View 6 Replies View Related

C++ ::  member Function Of Class

Jun 16, 2013

whether i can define a member function inside a class or not in C++. Is the following code is legal?

#include<iostream> using namespace std;
class adder {
private:
int a;
int b;
int c;
int answer;
public:

[code]....

View 6 Replies View Related

C++ :: Possible To Have Bitset Member In A Class?

Jan 18, 2013

I am looking to have a member of type bitset in a C++ class. Is it possible ?

Something like below:

Class abc
{
bitset<32> var;
}

View 4 Replies View Related

C++ :: Where To Put Non Class Member Functions

Sep 27, 2013

If you are doing some big program, usually, how do you organize the files? Put the class and its member in head file, but where to declare non member functions and where to define them? I don't want to put them all in one cpp file. If not, how to make them visible to the main cpp file?

View 4 Replies View Related

C/C++ :: Multiplying Two Member Of A Class?

May 1, 2014

we have to make a Invoice class which has a function called computeInvoiceAmount() which multiplies the price and the quantitiy which are private member of the class.

double computeInvoiceAmount(){
return quantity * pricePerItem;
}

This is my function, but if i compile it i get the following error:

"Invoice.cpp:53:11: error: "quantity" was not declared in this scope" and "Invoice.cpp:53:22: error: "pricePerItem" was not declared in this scope"

I tried it with the get functions but i get the same error.

Full code is here: [URL]

View 1 Replies View Related

C++ :: Displaying Variables From A Function Class

Feb 9, 2013

I am trying to display a variable from a class function the code works in debug but the variable is not displayed. Here is my code so far.

#include <iostream>
#include <string>
using namespace std;
class dayType {
public:
string day;
void setday(string);

[Code] .....

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved