Ive pasted the code for actor.cpp, date.cpp and the main function. This is alot of stuff I pasted but I believe it is necessary to show my problem. At first I get the date of the individuals birth and that gets displayed and everything turns out as it should. When i try to get that date back again in the Actor class by the code inception = dateInception.getCharacteristics();
I get a result that is like 123441234/1234234234/1234234. So according to my little understanding of classes I created an object on the stack in my main. Then because the code stopped using the object, the object was automatically dumped. So when i call dateInception.getCharacteristics(); for the Date class i get a number that has day, month and year set to random values. How do I solve this problem, I tried using pointers but that wasn't working for me.

Actor.cpp

#include "Actor.h"
#include "Point3D.h"
#include "Date.h"
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

Actor::Actor(){
	inception = dateInception.getCharacteristics();
	//death = dateDeath.getCharacteristics();
	location = location3D.distanceFrom();
	test = false;
	name = "unknown";
	maxSpeed = 0;
	height = 0;
	weight = 0;
	tendency = 0;
}

//returns name of type string

string Actor::getName() const{
	return name;
}

//returns maxSpeed of type double

double Actor::getMaxSpeed() const{
	return maxSpeed;
}

//returns weight of type integer

int Actor::getWeight() const{
	return weight;
}

//returns height of type integer

int Actor::getHeight() const{
	return height;
}

//returns tendency of type double

double Actor::getTendency() const{
	return tendency;
}
//returns dateInception of type Date

string Actor::getInception() const{
	return inception;
}

//returns dateDeath of type Date

string Actor::getDeath() const{
	return death;
}

//returns location of type Point3D

double Actor::getLocation() const{
	return location;
}

//tests and sets the value of the individual's tendency

bool Actor::validityTendency(int testTendency){
	if(testTendency == -1 || testTendency == 1){
		tendency = testTendency;
		test = true;
	}
	else
		test = false;
	return test;
}

//tests and sets the value of the individual's height

bool Actor::validityHeight(int testHeight){
	if(testHeight <= 1000 && testHeight >= 0){
		height = testHeight;
		test = true;
	}
	else
		test = false;
	return test;
}

//test and sets the value of the individual's weight

bool Actor::validityWeight(int testWeight){
	if(testWeight <= 1000 && testWeight >= 0){
		weight = testWeight;
		test = true;
	}
	else
		test = false;
	return test;
}
string Actor::getCharacteristics(){

	//Uses get function to return the values of Actor members

	tendency = getTendency();
	height = getHeight();
	weight = getWeight();
	maxSpeed = getMaxSpeed();
	name = getName();

	//Opens a stringstream and allows integers to be displayed as a string

	stringstream out;

	string dateString;

	//stores integers and strings into a string out stream

	out << "The individual's name is " << name << ".  "  << name << " is an " << tendency << " character."
			<< endl << name << " is " << height << " feet tall and weighs " << weight << " lbs.  " << endl
			<< name << " is located " << location << " feet from the origin.  " << endl
			<< name << " was born on " << inception << " and died on " << death << ".  "  << endl;

	//displays the string using the command out instead of cout

	out >> dateString;

	//ensures the method returns the full string

	dateString = out.str();

	return dateString;
}


Date.cpp

#include "Actor.h"
#include "Date.h"
#include <iostream>
#include <sstream>
using namespace std;

	//constructor that does nothing

	Date::Date(){
		test = false;
	}

	//returns day of birth

	int Date::getDay() const{
		return day;
	}

	//returns month of birth

	int Date::getMonth() const{
		return month;
	}

	//returns year of birth

	int Date::getYear() const{
		return year;
	}

	//Mutator method that checks validity of the day and sets day

	bool Date::validityDay(int testDay){
		if(testDay > 0 && testDay < 31){
			day = testDay;
			test = true;
		}
		else
			test = false;
		return test;
	}

	//Mutator method that checks validity of the month and sets month

	bool Date::validityMonth(int testMonth){
		if(testMonth > 0 && testMonth < 13){
			month = testMonth;
			test = true;
		}
		else
			test = false;
		return test;
	}

	//Mutator method checks validity of the year and sets year

	bool Date::validityYear(int testYear){
		if(testYear > 0 && testYear < 100000){
			year = testYear;
			test = true;
		}
		else
			test = false;
		return test;
		}

	//Collects day, month, year of birth and formats them for the appropriate display

	string Date::getCharacteristics(){

		string brackets = "/";

		//Opens a stringstream and allows integerss to be displayed as a string

		stringstream out;

		string dateString;

		//stores integers and strings into a string out stream

		out << month << brackets << day << brackets << year;

		//displays the string using the command out instead of cout

		out >> dateString;

		return dateString;
	}


Main function


#include "Actor.h"
#include "Point3D.h"
#include "Date.h"

int main(){

	//Value that returns true or false in the form of 0 or 1

	bool test;

	//Sets up a constructor and Date object to use in the Date class

	Date individualBirth;

	//creates an object to be implemented in the Actor class

	Actor characterAttributes;

	//Queries the user for the day of the month the individual was born

	cout << "On what day was your individual born?(DD)" << endl;
	int setDay;
	cin >> setDay;

	test = individualBirth.validityDay(setDay);

	while(test == 0){

		cout << "On what day was your individual born?(DD)" << endl;
		cin >> setDay;

		test = individualBirth.validityDay(setDay);
	}

	//Queries the user for the month the individual was born on

	cout << "On what month was your individual born?(MM)" << endl;
	int setMonth;
	cin >> setMonth;

	//calls mutator function and returns a boolean value

	test = individualBirth.validityMonth(setMonth);

	//Makes sure user enters a legitimate month within 1 to 12

	while(test == 0){

		cout << "On what month was your individual born?(MM)" << endl;
		cin >> setMonth;

		test = individualBirth.validityMonth(setMonth);
	}

	//Queries the user for the year the individual was born on
	//Any year is possible!

	cout << "What year was your individual born?(YYYY)" << endl;
	int setYear;
	cin >> setYear;

	//calls mutator function and returns a boolean value

	test = individualBirth.validityYear(setYear);

	//Makes sure user enters a legitimate month within 0 to 100000 (arbitrary value)

	while(test == 0){
		cout << "What year was your individual born?(YYYY)" << endl;
		cin >> setYear;

		test = individualBirth.validityYear(setYear);
	}
	//Displays date of birth of the individual in correct format

	cout << individualBirth.getCharacteristics() << endl;

	//Queries user for the temperament of the individual

	cout << "Enter 1 if the individual is good or -1 if the individual is bad:" << endl;
	int setTendency;
	cin >> setTendency;

	test = characterAttributes.validityTendency(setTendency);

	//Makes sure user typed in a 1 or a -1 for the user's temperament

	while(test == 0){

		cout << "Please enter the character's temperament by typing 1 or -1 in uppercase then press enter" << endl;
		cin >> setTendency;

		test = characterAttributes.validityTendency(setTendency);
	}

	//Queries user for the height of the individual

	cout << "What is the height of the individual in feet?" << endl;
	int setHeight;
	cin >> setHeight;

	test = characterAttributes.validityHeight(setHeight);

	//Makes sure user typed in an integer between 0 and 1000

	while(test == 0){
		cout << "What is the height of the individual in feet?" << endl;
		cin >> setHeight;

		test = characterAttributes.validityHeight(setHeight);
	}

	//Queries user for the weight of the individual

	cout << "What is the weight of the individual in pounds?" << endl;
	int setWeight;
	cin >> setWeight;

	test = characterAttributes.validityWeight(setWeight);

	//Makes sure user typed in an integer between 0 and 1000

	while(test == 0){
		cout << "What is the weight of the individual in pounds?" << endl;
		cin >> setWeight;

		test = characterAttributes.validityWeight(setWeight);
	}

	cout << characterAttributes.getCharacteristics();

	return 0;
}

You haven't shared the class definitions (that I can see). Unless dateInception is a member of Actor, you should be getting a warning from your compiler about using an undefined object.

Isn't this the same question you asked in your other thread?

It is the same question but I can't figure out why the variables are changing back to random values. the object is declared as Date dateInception. Any suggestions i'm still not getting it? Here are the header files

Actor Header File

#ifndef ACTOR_H_
#define ACTOR_H_

#include "Point3D.h"
#include "Date.h"
#include <iostream>

class Actor{
public:
	Actor();
	string getName() const;
	double getMaxSpeed() const;
	int getWeight() const;
	int getHeight() const;
	double getTendency() const;
	string getInception() const ;
	string getDeath() const;
	double getLocation() const;
	bool validityWeight(int testWeight);
	bool validityHeight(int testHeight);
	bool validityTendency(int testTendency);
	string getCharacteristics();

private:
	string inception, death, name;
	double maxSpeed, location, tendency;
	int height, weight;
	Date dateInception;
	Date dateDeath;
	Point3D location3D;
	bool test;
};
#endif /* ACTOR_H_ */

Date Header File

#ifndef DATE_H_
#define DATE_H_

#include <iostream>
using namespace std;

class Date{

	public:
		Date();
		int getDay() const;
		int getMonth() const;
		int getYear() const;
		bool validityDay(int testDay);
		bool validityMonth(int testMonth);
		bool validityYear(int testYear);
		string getCharacteristics();
	private:
		int day, month, year;
		bool test;
};

#endif /* DATE_H_ */

In order for dateInception to be of any use to you, you must initialize it. I don't see where you are initializing it. I see where you are creating an object of the Date class you are calling "individualBirth". The problem is this object, which is local to main(), has absolutely nothing to do with the object dateInception which is a member of Actor.

Think of it in terms of architecture and building construction. A class is a "blueprint" for a building, it has no physical existance, it is only a concept. There will be no physical version until a building is constructed from that "blueprint". In the same manner, an object is a physical instance of a class. You can build more than one instance from the same blueprint, and each instance will have it's own attributes. What happens in or to one of those instances does not affect the status of the other instances. The individual physical instances are all completely independent of each other. (There are some exceptions, but I'm not going to confuse you with those details right now.)

What you are trying to do requires overloading the assignment operator and writing a copy constructor for the Date class. It also requires writing access methods that are part of the Actor class.

I think you may want to try some simpler projects to get a better understanding of OOP concepts before you finish this.

This is one of the header files from something that I'm working on right now for my employer. Have a look through it, you'll see some of what I'm talking about:

#ifndef PROJECT_H
#define PROJECT_H

#include "ITAHead.h"  //add Universal ITA header file

const string DEFAULT_MAJOR_NUMBER = "empty";
const string DEFAULT_PROJECT_NAME = "none";
const string DEFAULT_PATH = "**********";

//define constants used by ProjectNumber and Project
const int MAJOR_NUMBER_LENGTH = 5;
const int START_OF_SUFFIX = MAJOR_NUMBER_LENGTH;

class ProjectNumber {
private:
  string MajorNumber;
  string Suffix;

public:
  ProjectNumber();                                   //default constructor
  ProjectNumber(const string &);                     //specified constructor
  ProjectNumber(const ProjectNumber &);              //copy constructor
  string FormattedNumber() const;                    //return formatted PN to caller
  void setNumber(const string &newNumber);           //set the value of the PN
  ProjectNumber &operator= (const ProjectNumber &);  //overload assignment operator
};

class Project {
private:
  ProjectNumber m_Number;   //a ProjectNumber object to manage the project number
  string m_Name;            //the name of the project
  string m_ProjectPath;     //the file path

public:
  Project();                   //default constructor
  Project(const string &);     //specified constructor
  Project(const Project &);    //copy constructor
  string getNumber() const;    //returns the project's formatted PN
  //TODO:
};

#endif //PROJECT_H

If you want to see the implementation of a specific method, let me know.

Edited 6 Years Ago by Fbody: n/a

>> ...When i try to get that date back again in the Actor class
>> by the code inception = dateInception.getCharacteristics();
>> I get a result that is like 123441234/1234234234/1234234.

By looking at the code (your first post), this line you mention, only exists within the Actor's constructor, meaning that it returns the newly constructed dateInception 's data - do you have false expectations here? Date::getCharacteristics() does not work as you have intended ..

Change

string Date::getCharacteristics()
{
    ...

    out >> dateString;

to

string Date::getCharacteristics()
{
    ...

    // Stuff the stringstream content into the string
    dateString = out.str();

    return dateString;
}

or maybe drop dateString altogether and simply

string Date::getCharacteristics()
{
    ...

    // Return the string
    return out.str();
}

[EDIT]
Just noticed that Actor::getCharacteristics() also has similar faulty usage of stringstream.

Edited 6 Years Ago by mitrmkar: n/a

It's always great when you think you have a concept understood and then you realize that you basically know nothing...ahhh this is a ball buster.

Hey I ended up just looking at the OOP design from scratch and I was able to come up with the correct way of using the object. Thanks FBody

This question has already been answered. Start a new discussion instead.