Returning an object in another class file - c++

Try this
Date.h
#ifndef DATE_H
#define DATE_H
#include<string>
using namespace std;
class Date{
public:
Date(int month, int day, int year);
int getMonth() const;
int getDay() const;
int getYear() const;
private:
int month;
int day;
int year;
};
#endif
Date.cpp
#include "Date.h"
#include<string>
using namespace std;
Date::Date(int month, int day, int year) {
this->month = month;
this->day = day;
this->year = year;
}
int Date::getMonth() const{
return month;
}
int Date::getDay() const{
return day;
}
int Date::getYear() const{
return year;
}
Appointment.cpp
#include "Appointment.h"
#include "Date.h"
#include<sstream>
#include<string>
using namespace std;
Appointnment::Appointment(String description, int month, int day, int yr, int hr, int min)
{
this->description = description;
this->month = month;
this->day = day;
this->yr = yr;
this->hr = hr;
this->min = min;
}
void Appointinment::getDate()
{
//cannot change calling object nor its date object member, just return it
}
}
I'm trying to implement the getDate() function, but I'm having trouble understanding how to return a Date given that this class is specified as an appointment. Are there any resources I can look at to tackle this specific problem?

You code looks good. But when return soemthing, you need either a pointer to Data, or a refernce to Date, or straight object.
Either of following will work:
Date Appointinment::getDate() const
const Date& Appointinment::getDate() const
const Date* Appointinment::getDate() const
Judge from context, maybe first one is what you want.

const Date& Appointinment::getDate()
{
return Date(this->month, this->day, this->yr);
}
this is the most common way.

Related

Is it possible to call cpp file into main, without using header?

Is it possible to call cpp file into main, without using header?
I tried in VS 2019 something like that:
main.cpp
#include<iostream>
int main()
{
Date birthday{3,2,2};
birthday.getDay();
std::cout << "User, input the day";
int u_day = 0;
std::cin >> u_day;
birthday.print(u_day);
return 0;
}
another.cpp
#include<iostream>
class Date
{
private:
int m_year;
int m_month;
int m_day;
public:
Date() = default;
Date(int year, int month, int day) {
setDate(year, month, day);
}
void setDate(int year, int month, int day) {
m_year = year;
m_month = month;
m_day = day;
}
int getYear() { return m_year; }
int getMonth() { return m_month; }
int getDay() { return m_day; }
void print(int day) {
m_day = day;
std::cout << m_day;
}
};
Now there are a lot of C2056 errors,
When I add Date::Date birthday{3,2,2}; in the main.cpp
I get E0276, E0065, C2653,C2065,C2146

How to create a test run/plan for a C++ Program? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hi I have a c++ class called Date. The codes are below. How to create a test run for the class? Am very much new to C++. I have searched a lot but couldn't able find any fruitful/useful websites for that. Please someone can help? Thanks in advance.
date.h
#ifndef H_date
#define H_date
#include <iostream>
using namespace std;
class Date{
friend ostream &operator << (ostream & os, const Date &);
friend istream &operator >> (istream & is, Date &);
public:
Date();
Date(int day, int month, int year);
void setday(int day);
void setmonth(int month);
void setyear(int year);
void setDate(int day, int month, int year);
int getday() const;
int getmonth() const;
int getyear() const;
void print() const;
protected:
int day;
int month;
int year;
};
#endif
date.cpp
#include <iostream>
#include "date.h"
using namespace std;
Date::Date()
{
day = 0;
month = 0;
year = 0;
}
Date::Date(int newDay, int newMonth, int newYear)
{
day = newDay;
month = newMonth;
year = newYear;
}
void Date::setday(int newDay)
{
day =newDay;
}
void Date::setmonth(int newMonth)
{
month = newMonth;
}
void Date::setyear(int newYear)
{
year = newYear;
}
void Date::setDate(int newDay, int newMonth, int newYear)
{
day =newDay;
month = newMonth;
year = newYear;
}
int Date::getday()const
{
return day;
}
int Date::getmonth()const
{
return month;
}
int Date::getyear()const
{
return year;
}
void Date::print()const
{
cout << day << ":" << month<< ":" << year <<endl;
}
ostream& operator<< (ostream& osObject, const Date& date1)
{
osObject << date1.day
<< ":" <<date1.month
<< ":" << date1.year;
return osObject;
}
istream& operator>> (istream& isObject, Date& date1)
{
isObject>>date1.day>>date1.month>>date1.year;
return isObject;
}
For unit tests, you would typically use a unit-test framework such as CppUnit:
For each class, you create a series of test;
For each method to be tested, you develop a dedicated test;
Each test verifies some assertions using functions or macros such as CPPUNIT_ASSERT, CPPUNIT_FAIL, CPPUNIT_ASSERT_THROW;
It's often useful to make the tester class friend to the tested class.
Here is an example for testing your constructors:
void DateUnitTest::ConstructorTests
{
// Test default-ctor
{
Date date;
CPPUNIT_ASSERT ( date.day == 0 );
CPPUNIT_ASSERT ( date.month == 0 );
CPPUNIT_ASSERT ( date.year == 0 );
}
// Test ctor with args
{
Date date(31,12,2015);
CPPUNIT_ASSERT ( date.day == 31 );
CPPUNIT_ASSERT ( date.month == 12 );
CPPUNIT_ASSERT ( date.year == 2015 );
}
}

I'm trying to learn how to properly separate class header and code

I have a class project that I did from college that "works", but isn't constructed properly.
It has a Date class and a Person class.
In my "working" code, all of the class data (constructors, members, and functions) are contained within a single header file for each class (a separate file for each class).
The program loads text file data (Persons, presidents and actors) into a vector, sorts, filters and prints the data on the console, and saves it to a file.
In the Person class constructor (and functions), the Date class is instantiated for a "birthdate" object. Then the birthday is used in the program.
The problem I'm having is this:
I can easily instantiate "Date" in the Person constructor and members, if all the class code is in a header file for each class, AND I make the Date the "Parent" class for Person. But if I don't do both of these things, then "Person" doesn't know anything about "Date" and cannot instantiate anything from Date.
If I use an #include "Date.h" in Person.h, this creates even more problems and doesn't work either.
Of course, "Date" is not a proper parent class for "Person", but it was the only way I could hack the code to make it work lol. When I first coded this years ago in college, I never did figure out how to divide the class code properly in so that it would compile and run. The "working code" with all the class code in a header file is my hack. I want to learn how to do it the right way.
I have pasted in the bare minimum example below of what "works" and what doesn't. Any tips to make this code work with the classes divided into header and .cpp files would be much appreciated.
What works:
class Date {
private:
int month;
int day;
int year;
public:
Date::Date() {}
virtual ~Date() {}
Date( int aMonth, int aDay, int aYear ) {
this->month = aMonth;
this->year = aYear;
this->day = aDay;
}
// "Getter" functions for the Date class
int getMonth(){return this->month;}
int getYear() {return this->year;}
int getDay() {return this->day;}
// "Setter" functions for the Date class
void setDay( int aDay ){ this->day = aDay; }
void setMonth( int aMonth ) { this->month = aMonth; }
void setYear( int aYear ) { this->year = aYear; }
};
class Person : public Date{
private:
string title;
string firstName;
string lastName;
Date birthDate;
public:
Person::Person() {}
Person(string title, string firstName, string lastName, Date birthDay);
virtual ~Person() {}
//"Getter" functions for the Person class
string getTitle() { return title; }
string getFirstName() { return firstName; }
string getLastName() { return lastName; }
Date getBirthDay() { return birthDate; }
//"Setter" functions for the Person class
void setTitle(string Title) { this->title = Title; }
void setFirstName(string fName) { this->firstName = fName; }
void setLastName (string lName) { this->lastName = lName; }
void setBirthday (Date aBirthday) { this->birthDate = aBirthday; }
};
What doesn't work (Doesn't compile):
Date.h
class Date {
private:
int month;
int day;
int year;
public:
Date();
virtual ~Date() {}
Date( int aMonth, int aDay, int aYear );
//Getters and setters
int getMonth();
int getYear() ;
int getDay();
void setDay( int aDay );
void setMonth( int aMonth ) ;
void setYear( int aYear ) ;
};
Date.cpp
#include "Date.h"
Date::Date() {}
Date::Date( int aMonth, int aDay, int aYear ) {
this->month = aMonth;
this->year = aYear;
this->day = aDay;
}
int Date::getMonth(){return this->month;}
int Date::getYear() {return this->year;}
int Date::getDay() {return this->day;}
//"Setter" functions for the Date class
void Date::setDay( int aDay ){ this->day = aDay; }
void Date::setMonth( int aMonth ) { this->month = aMonth; }
void Date::setYear( int aYear ) { this->year = aYear; }
Person.h
#include <string>
class Person{
private:
string title;
string firstName;
string lastName;
Date birthDate;
public:
//Person::Person() {}
Person(string title, string firstName, string lastName, Date birthDay);
//"Getter" functions for the Person class
string getTitle() { return title; }
string getFirstName() { return firstName; }
string getLastName() { return lastName; }
Date getBirthDay() { return birthDate; }
//"Setter" functions for the Person class
void setTitle(string Title) { this->title = Title; }
void setFirstName(string fName) { this->firstName = fName; }
void setLastName (string lName) { this->lastName = lName; }
void setBirthday (Date aBirthday) { this->birthDate = aBirthday; }
};
Person.cpp
#include "Person.h"
#include <iostream>
using namespace std;
//Person class constructor with 0 arguments
Person::Person(string atitle, string afirstName, string alastName, Date abirthDay) {
title = atitle,
firstName = afirstName,
lastName = alastName,
birthday = abirthDay)
}
//"Getter" functions for the Person class
string Person::getTitle() { return title; }
string Person::getFirstName() { return firstName; }
string Person::getLastName() { return lastName; }
Date Person::getBirthDay() { return birthDate; }
//"Setter" functions for the Person class
void Person::setTitle(string Title) { this->title = Title; }
void Person::setFirstName(string fName) { this->firstName = fName; }
void Person::setLastName (string lName) { this->lastName = lName; }
void Person::setBirthday (Date aBirthday) { this->birthDate = aBirthday; }
Operating system notes
I'm using MS Visual Studio 2012 Update 4 on a Windows 7 PC.
To prevent redefinition errors, add include guards to header files. To be portable, use:
#if !defined(THE_HEADER_NAME_H)
#define THE_HEADER_NAME_H
// Usual code and declarations here.
#endif
In Visual Studio, you can use:
#pragma once
// Usual code and declarations here.
In headers, don't write (at global scope level)
using namespace XXXXX
because that will cause all files including yours to also use namespace XXXXX which they may not want imposed upon them. If you use types that are in a namespace, type out the entire type name with namespace, so use:
std::string getTitle()
(In a cpp file, you can have 'using namespace XXXXX' after any #includes)
You have 2 problems IMHO.
First is the guards problem that Scott Langham's answer will solve.
The second is a problem of namespaces.
I assume that in your working source, you have (directly on indirectly via stdafx.h or another include) :
#include <string>
using namespace std;
It is not recommended to have using namespace std; in a .h header file ... but that means that you should use std::string instead of string:
date.h
class Date {
private:
int month;
int day;
int year;
public:
Date::Date() { } //Date class constructor with 0 arguments
virtual ~Date() {} //Date class destructor
Date( int aMonth, int aDay, int aYear ) { //Date class constructor with 3 arguments
this->month = aMonth; // Set the private member data with the argument data
this->year = aYear;
this->day = aDay;
}
//"Getter" functions for the Date class
int getMonth(){return this->month;}
int getYear() {return this->year;}
int getDay() {return this->day;}
//"Setter" functions for the Date class
void setDay( int aDay ){ this->day = aDay; }
void setMonth( int aMonth ) { this->month = aMonth; }
void setYear( int aYear ) { this->year = aYear; }
};
person.h (string -> std::string and remove method implementations)
#include <string>
#include "Date.h"
class Person : public Date{
private: //Private data members
std::string title;
std::string firstName;
std::string lastName;
Date birthDate;
public:
Person::Person() {} //Person class constructor with 0 arguments
Person(std::string title,
std::string firstName,
std::string lastName,
Date birthDay); //Person class constructor with 4 arguments
virtual ~Person(){} //Person class destructor
//"Getter" functions for the Person class
std::string getTitle();
std::string getFirstName();
std::string getLastName();
Date getBirthDay();
//"Setter" functions for the Person class
void setTitle(std::string Title);
void setFirstName(std::string fName);
void setLastName (std::string lName);
void setBirthday (Date aBirthday);
};
person.cpp (fixed includes and constructor)
#include <string>
#include "Person.h"
#include <iostream>
using namespace std;
//Person class constructor with 0 arguments
Person::Person(string atitle, string afirstName, string alastName, Date abirthDay) {
title = atitle,
firstName = afirstName,
lastName = alastName,
birthDate = abirthDay;
}
//"Getter" functions for the Person class
string Person::getTitle() { return title; }
string Person::getFirstName() { return firstName; }
string Person::getLastName() { return lastName; }
Date Person::getBirthDay() { return birthDate; }
//"Setter" functions for the Person class
void Person::setTitle(string Title) { this->title = Title; }
void Person::setFirstName(string fName) { this->firstName = fName; }
void Person::setLastName (string lName) { this->lastName = lName; }
void Person::setBirthday (Date aBirthday) { this->birthDate = aBirthday; }
It can even be used without guards, but guard are anyway a good practice.
In fact as Date.h is included in Person.h it should really have a guard - not shown here for simplicity

Operator overloading error?

I keep getting an compiler error with my program in Visual Studio Express 13
I commented the 2 lines in my code where the compiler error is showing up
Date.cpp
#include "Date.h"
using namespace std;
Date::Date(int d, int m, int y) : day(d), month(m), year(y)
{}
Date::Date() : day(0), month(0), year(0)
{}
const int Date::getDay() { return day; }
const int Date::getMonth() { return month; }
const int Date::getYear(){ return year; }
bool Date::operator<(const Date dOther)
{
return (year < dOther.year) || (year == dOther.year && month < dOther.month)
|| (year == dOther.year && month == dOther.month && day < dOther.day);
}
string Date::toString() //Error: Declaration is incompatible with...declared at line 21...Date.h
{
string s = month + "-" + day; s+="-" + year;
return s;
}
ofstream& operator<<(ofstream& out, Date& d)
{
string s = d.toString();
out.write(s.c_str(), s.size());
return out;
}
void Date::operator=(string s) //no instance of overloaded function "Date::operator=" matches the specified type
{
stringstream stream;
stream << s;
stream >> month;
stream >> day;
stream >> year;
}
Date.h
#ifndef DATE_CLASS
#define DATE_CLASS
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
class Date
{
private:
int day, month, year;
public:
Date();
Date(int, int, int);
const int getDay();
const int getMonth();
const int getYear();
string toString();
bool operator<(const Date);
friend ofstream& operator<<(ofstream& out, Date&d);
void operator=(string);
};
#endif
I have no idea why these errors are showing up. Is it a problem with operator overloading? or something with visual studio(for some reason if I delete some code in Date.cpp, the compiler errors disappear)?
You have namespaces mismatch. In the header you do not use the std namespace prefix before string thus the compiler can't find the type string that you need.

How to return a struct from a class in C++?

I have a problem in returning a struct from a method declared in a class. The struct i is composed by 3 integers: day, month and year.
I set these three values using a method in the class.
struct dmy{
int day, month, year;
};
dmy c;
Then I have a method to return the full struct
dmy Date::getS(){
return c;
}
But I get lots of errors in compiling. What should I do?
I've also read Is it safe to return a struct in C or C++? but I haven't solved my issue.
The program is composed from main.cpp, Difference.h and Date.h
Errors in Date.h :
[Error] 'dmy' does not name a type
Main
using namespace std;
#include <iostream>
#include "Date.h"
int main(int argc, char** argv) {
Date date1;
Date date2;
cout<<"Insert first date\n";
date1.setAll();
return 0;
}
Date.h
class Date{
public:
Date(){
}
~Date(){
}
void setAll();
struct dmy{
int day, month, year;
};
dmy c;
dmy getS();
private:
void setDay();
void setMonth();
void setYear();
};
void Date::setAll(){
setDay();
setMonth();
setYear();
}
void Date::setDay(){
do{
cout<<"Type day: ";
cin>>c.day;
}while(0<c.day<31);
}
void Date::setMonth(){
//didn't right the checking again
cout<<"Type month: ";
cin>>c.month;
}
void Date::setYear(){
cout<<"Type year: ";
cin>>c.year;
}
dmy Date::getS(){
return c;
}
NOTE: The struct was called 'i'
Example
#include <cstdio>
#include <cstdlib>
struct DateInfo {
int day;
int month;
int year;
};
class Date {
public:
Date(const int day, const int month, const int year) : m_info( { day, month, year } )
{
//n/a
}
DateInfo get_info( void ) const
{
return m_info;
}
private:
DateInfo m_info;
};
int main( int, char** )
{
Date date( 29, 02, 1984 );
DateInfo info = date.get_info( );
printf( "%i\n", info.year );
return EXIT_SUCCESS;
}
Build
g++ -std=c++11 -o date date.cpp
instead of
dmy Date::getS(){
return c;
}
write
Date::dmy Date::getS(){
return c;
}
since "dmy" is not in the global namespace.
Try this:
class C1
{
public:
struct S1
{
int a,b,c;
};
S1 f1() { S1 s; return s; }
};
void main()
{
C1 c;
C1::S1 s;
s=c.f1();
}
C1 - class
C1::S1 - struct inside class C1
C1::f1 - function returning C1::S1
I doubt that your source code is compiled in the C style other that C++ style. You could try to change the file extension to ".cpp" instead of ".c" and then gcc will do it's job in C++ style.