inheritance in c++ ( C::B ) - c++

i am new to c++ and not skillful in English!
i have searched web and read same posts on this site but did not worked for me!
its a wonder that inheritance used in this part of my project seems correct to me but get these error from C::B 13.12 with GNU GCC Compiler under Windows 8.1 :
||=== Build: Debug in test (compiler: GNU GCC Compiler) ===|
D:\AirportProject\Airport\aircrew.h|8|error: expected class-name before '{' token|
D:\AirportProject\Airport\aircrew.cpp||In constructor 'Aircrew::Aircrew(const string&, const string&, long long int, const Date&, long long int, const Date&)':|
D:\AirportProject\Airport\aircrew.cpp|5|error: class 'Aircrew' does not have any field named 'Person'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
this is my code:
person.h:
#include "date.h"
using namespace std;
class Person{
public:
Person( const string&, const string&, long long, const Date& );
void set( const string&, const string&, long long, const Date& );
private:
char name [31];
char last_name [31];
long long national_code ; //long long for storing a 10 digit number
Date birthday;
};
person.cpp:
#include "person.h"
#include <string>
using namespace std;
Person::Person( const string& n, const string&ln, long long ncode, const Date& D )
{ set( n, ln, ncode, D ); }
//set data members of aircrew
void Person::set( const string& n, const string&ln, long long ncode, const Date& D )
{
birthday = D ;
strncpy( name, n.c_str(), 30 ); //converting string to char array
strncpy( last_name, ln.c_str(), 30 );
national_code= ncode;
}
aircrew.h:
#include <string>
#include "person.h"
#include "date.h"
using namespace std;
class Aircrew: public Person
{
public:
//copy-constructor
Aircrew( const string&, const string&, long long, const Date&,
long long, const Date& );
//Set member data
void set( long long, const Date& );
private:
//long long used to be sure about 10digit numbers and more
long long personal_code;
// date of employment
Date employ_date;
};
aircrew.cpp:
#include "aircrew.h"
using namespace std;
Aircrew::Aircrew( const string& n, const string& ln, long long ncode,
const Date& D_b, long long pcode,const Date& D_e )
:Person( n, ln, ncode, D_b ) //base class constructor
{set( pcode, D_e );}
void Aircrew::set(long long pcode, const Date& D_e )
{
personal_code = pcode;
employ_date = D_e; //assignment operator( = ) is overloaded for Date class
}
date.h
class Date
{
public:
Date ( int=1, int=1, int=1900 ); //Default constructor
void set( int, int, int );
int getForSearch() const ;
void print() const ;
//assignment operator( = ) is overloaded for Date class
Date& operator=( const Date& D )//assignment operator(=) can NOT be defined as friend
{
day = D.day;
month = D.month;
year = D.year;
return *this;
}
private:
int day, month, year;
};
date.cpp
#include <iostream>
#include "date.h"
using namespace std;
//Default constructor
Date::Date( int d, int m, int y) { set( d, m, y ); }
//set() function sets the entered date if its valid
void Date::set( int d, int m, int y )
{
day=( d>0? d: 1);
month=( m>0? m: 1);
year= ( y>0? y: 1900);
}
//getForSearch () returns an integer like yyyymmdd for easy search
int Date::getForSearch () const
{
return ((year*100 + month)*100 + day);
}
//print Date in the form of mm/dd/yyyy
void Date::print () const
{
cout << " " << month << "/" << day << "/" << year;
}
and also an empty main.cpp!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << right <<setw( 30 ) << "\nAIRPORT MANAGER" << endl;
return 0;
}
Any help will be appreciate :)

Related

Set and get methods in a class for objects

I have a class Result which takes two other classes UNIT and Date as object parameters, so that I can store the values in them for use in those classes. I will put down what Ive got so far, and a few of the errors I get are as follows.
error: prototype for 'int Result::GetUnit() const' does not match any in class 'Result'|
lab2\Result.h|17|error: candidate is: Result Result::GetUnit() const|
lab2\Result.cpp|66|error: prototype for 'int Result::GetDate()' does not match any in class 'Result'|
lab2\Result.h|18|error: candidate is: Result Result::GetDate() const|
lab2\Result.cpp|73|error: 'SetResult' was not declared in this scope|
error: 'SetResult' was not declared in this scope|
For the error not declared in this scope, after researching I understand that I need to define the functions before the first call is made. I have done that in the .cpp file but I still get the error. What am I doing wrong?
Result.h:
#ifndef RESULT_H
#define RESULT_H
#include <iostream>
#include <string>
#include "UNIT.h"
#include "Date.h"
//const unsigned ResultSize = 10;
using namespace std;
class Result
{
public:
Result(){};
Result(UNIT unitobj1, unsigned marks1, Date dateobj1);
Result GetUnit() const;
Result GetDate() const;
void SetDate(Date dateobj1);
void SetUnit(UNIT unitonj1);
void SetMarks( unsigned marks1 );
unsigned GetMarks() const;
void SetCredits( unsigned cred );
unsigned GetCredits() const;
string GetID() const;
void SetID(string idd);
void SetResult(istream & input);
void GetResult(ostream & os);//unsigned GetUnit() const;
private:
UNIT unitobj;
Date dateobj;
string id;
int credits;
unsigned marks;
};
inline unsigned Result::GetCredits() const
{
return credits;
}
ostream & operator <<( ostream & os, const Result & S);
istream & operator >>( istream & input, Result & S);
#endif // RESULT_H
Result.cpp:
#include "Result.h"
#include "UNIT.h"
#include "Date.h"
Result::Result(UNIT unitobj1, unsigned marks1, Date dateobj1)
{
unitobj = unitobj1;
marks = marks1;
dateobj = dateobj1;
}
void Result::SetResult(istream &input){
UNIT unitobj1;
unsigned marks1;
Date date1;
input >> unitobj1 >> marks1 >> date1;
SetUnit(unitobj1);
SetMarks(marks1);
SetDate(date1);
}
void Result::GetResult(ostream &os){
os << GetUnit() < " Marks: " << GetMarks() << '\n' << GetDate() << '\n';
}
void Result::SetUnit(UNIT unitobj1){
unitobj = unitobj1;
}
void Result::SetMarks(unsigned marks1){
marks = marks1;
}
void Result::SetDate(Date dateobj1){
dateobj = dateobj1;
}
Result::GetUnit() const{
return unitobj;
}
inline unsigned Result::GetMarks() const{
return marks;
}
Result::GetDate(){
return dateobj;
}
istream & operator >>( istream & input, Result & S)
{
SetResult(input);
return input;
}
ostream & operator <<( ostream & os, const Result & S)
{
GetResult(os);
return os;
}
Date.h:
#if !defined(_DATE_H)
#define _DATE_H
#include <iostream>
#include <string>
using namespace std;
class Date {
public:
Date();
Date(unsigned day1, string month1, unsigned year1);
void SetDay(unsigned day1);
void SetMonth(string month1);
void SetYear(unsigned year1);
unsigned GetDay() const;
string GetMonth() const;
unsigned GetYear() const;
void SetDate(istream &input);
void GetDate(ostream & os);
private:
unsigned day;
string month;
unsigned year;
};
ostream & operator <<(ostream & os, const Date & D);
istream & operator >>(istream & input, Date & D);
#endif //_DATE_H
Date.cpp:
//
//
// Generated by StarUML(tm) C++ Add-In
#include "Date.h"
Date::Date(unsigned day1, string month1, unsigned year1) {
day = day1;
month = month1;
year = year1;
}
void Date::SetDay(unsigned day1) {
day = day1;
}
void Date::SetMonth(string month1) {
month = month1;
}
void Date::SetYear(unsigned year1) {
year = year1;
}
inline unsigned Date::GetDay() const {
return day;
}
string Date::GetMonth() const {
return month;
}
inline unsigned Date::GetYear() const {
return year;
}
void Date::SetDate(istream &input){
unsigned day1;
string month1;
unsigned year1;
input >> day1 >> month1 >> year1;
SetDay(day1);
SetMonth(month1);
SetYear(year1);
}
void Date::GetDate(ostream &os){
os << " Date: " << GetDay() << " " << GetMonth() << " " << GetYear();
}
istream & operator >>( istream & input, Date & D) {
SetDate(input);
return input;
}
ostream & operator <<( ostream & os, const Date & D) {
GetDate(os);
return os;
}
UNIT.h:
#ifndef UNIT_H
#define UNIT_H
#include <iostream>
#include <string> // C string library
using namespace std;
const unsigned UnitNameSize = 10;
class UNIT
{
public:
UNIT();
UNIT( string nam, string idd, unsigned cred);
void SetName(string nam);
string GetName() const;
void SetMarks(unsigned marks1);
unsigned GetMarks();
void SetCredits(unsigned cred);
unsigned GetCredits() const;
string GetID() const;
void SetID(string idd);
void SetUnit(istream & input);
void GetUnit(ostream & os);
private:
string name;
string id;
unsigned marks;
int credits;
};
ostream & operator <<( ostream & os, const UNIT & U);
istream & operator >>( istream & input, UNIT & U);
#endif // UNIT_H
UNIT.cpp:
#include "UNIT.h"
#include <string>
UNIT::UNIT()
{
name[0] = '\0';
}
void UNIT::SetName(string nam)
{
name = nam;
}
string UNIT::GetName() const
{
return name;
}
void UNIT::SetID(string idd)
{
id = idd;
}
string UNIT::GetID() const
{
return id;
}
void UNIT::SetCredits(unsigned cred){
credits = cred;
}
inline unsigned UNIT::GetCredits() const{
return credits;
}
UNIT::UNIT( string nam, string idd,
unsigned cred)
{
name.replace(0, 10, nam );
id = idd;
credits = cred;
}
void UNIT::SetUnit(istream &input){
string nam;
string idd;
unsigned cred;
getline(input,nam, '\n');
getline(input,idd,'\n');
input >> cred;
SetName(nam);
SetID(idd);
SetCredits(cred);
}
void UNIT::GetUnit(ostream & os){
os << " Unit ID: " << GetID() << '\n'
<< " Unit Name: " << GetName() << '\n'
<< " Credits: " << GetCredits() << '\n';
}
istream & operator >>( istream & input, UNIT & U)
{
SetUnit(input);
return input;
}
ostream & operator <<( ostream & os, const UNIT & U)
{
GetUnit(os);
return os;
}
Note: I know that I can just make the overloaded operators methods of the class and declare them as friends, but I am trying to achieve this by using set and get methods. Any help would be very appreciated!
So, this is pretty straightforward, look at Result::GetUnit
Result::GetUnit() const {
return unitobj;
}
This method is missing a return type. Now look at unitobj
UNIT unitobj;
So it's clear that the return type should be UNIT, so the above method should be defined as
UINT Result::GetUnit() const {
return unitobj;
}
Now look at how you declared this method in your class
class Result
{
...
Result GetUnit() const;
Here you gave it a return type of Result where we've already seen that the return type should be UINT, so change the above to
class Result
{
...
UNIT GetUnit() const;
Result::GetDate has similar problems, here the return type should be Date but again you specified it as Result.
For the SetResult error you simply need to specify that you are calling the SetResult method on a Result object. The compiler thinks you are calling a global SetResult function (which doesn't exist)
Like this
istream & operator >>( istream & input, Result & S)
{
S.SetResult(input);
return input;
}
S.SetResult(input); tells the compiler that you want to call the SetResult method using the Result object refered to by S.
You can tell this by reading the error message carefully, it said 'SetResult' was not declared in this scope, not 'Result::SetResult' was not declared in this scope. As you said you have declared Result::SetResult, but your code wasn't calling that, it was trying to call a different function called SetResult and the compiler correctly reported that no such function was declared.

Get function that "gets" other get functions

So my challenge is: create a function that receives all of the parameters from the other get functions, like:
int get_day() const {return day;}
int get_month() const {return month;}
int get_year() const {return year;}
I want get_date() that receives all of the above gets. How can I do that? For example:
int get_date() const {
get_day();
get_month();
get_year();
}
You may pack the date like this:
int get_date() const {
return get_day() + 100 * get_month() + 10000 * get_year();
}
Note, that this is just integer, that looks like date. If you print today's date, it will be the number 20190423, which is just the number 20,190,423.
As I mentioned in the comments, it makes no sense to return a single int when you need your function to return 3 ints.
Just return an array like this:
#include <iostream>
#include <array>
struct Cal
{
typedef std::array<int,3> Date;
int day = 13;
int month = 7;
int year = 2019;
int getDay() const
{
return day;
}
int getMonth() const
{
return month;
}
int getYear() const
{
return year;
}
Date getDate() const
{
return {{getDay(),getMonth(),getYear()}};
}
};
int main()
{
Cal c;
for (auto &&i : c.getDate())
std::cout<< i <<" ";
std::cout<<std::endl;
}
The code outputs:
13 7 2019
Also, it's best if you simply returned the actual members instead of calling the getter functions. Besides the getDate() function is also a member of the class.
Date getDate() const
{
return {{day,month,year}};
}
Online code example: https://rextester.com/WPXX24681
you can use tm struct as in here: std::tm
std::time_t t = std::time(0);
std::tm* now = std::localtime(&t);
std::cout << (now->tm_year + 1900) << (now->tm_mon + 1) << (now->tm_mday);
don't reinvent the wheel

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.

Returning an object in another class file

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.

Out of memory when using sqlite3 [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am just beginning with programming in c++ (IDE is CodeBlocks).
I wrote two simple classes (Song and Metadata) and testing around with sqlite.
The Problem: My program seems to get insufficient memory: When I start the program it either crashes immediately or the sqlite3_open command returns the error "out of memory". When I test the SQLITE function without any other code, it works!
So this does not work:
#include <iostream>
#include "metadata.h"
#include "Song.h"
#include <cstdio>
#include "sqlite3.h"
using namespace std;
int main()
{
// Without this block it DOES work!
Metadata* tmpMeta = new Metadata("Eiffel 65", "Blue", "Europop");
Song* tmpSong;
tmpSong = new Song();
tmpSong->set_metadata(*tmpMeta);
// end of block
sqlite3 *db;
int rc;
rc = sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
cout << "Result: " << sqlite3_errmsg(db) << '\n';
sqlite3_close(db);
return 0;
}
Please see the Song and Metadata classes attached.
I do not know how to react to this error.
My environment:
- Windows 8, 64 bit
- Code::Blocks 10.05
- Compiler: GNU GCC Compiler
Thank you in advance!
Greetings
Sebastian
Song.h:
#include <iostream>
#include <cstring>
#include "metadata.h"
using namespace std;
class Song {
private:
Metadata metadata;
unsigned int iD;
char filename[400];
public:
//Constructors
Song( Metadata, unsigned int, const char*);
Song( );
//Methods
void set_metadata(Metadata& meta);
void set_id(unsigned int i);
void set_filename(const char* f);
Metadata get_metadata();
unsigned int get_id();
const char* get_filename();
};
Song.cpp:
#include <iostream>
#include "Song.h"
using namespace std;
Song::Song(Metadata m, unsigned int id, const char* f) {
metadata = m;
iD = id;
strncpy(filename, f, sizeof(filename)-1);
filename[sizeof(filename)] = '\0';
}
Song::Song( ) {
Metadata tmpMeta;
metadata = tmpMeta;
iD = 0;
strncpy(filename, "unknown", sizeof(filename) -1);
filename[sizeof(filename)] = '\0';
}
void Song::set_filename(const char* f) {
strncpy( filename, f, sizeof(filename)-1 );
filename[sizeof(filename)] = '\0';
}
void Song::set_id(unsigned int i) {
iD = i;
}
void Song::set_metadata(Metadata& meta) {
metadata = meta;
}
Metadata Song::get_metadata() {
return metadata;
}
const char* Song::get_filename() {
return filename;
}
unsigned int Song::get_id() {
return iD;
}
Metadata.h:
#include <iostream>
#include <cstring>
#ifndef _METADATA_H_
#define _METADATA_H_
using namespace std;
class Metadata {
private:
unsigned int trackNumber;
char artist[20];
char title[20];
unsigned int year;
char genre[20];
char album[20];
public:
Metadata(const char*, const char*, const char*);
const char* get_artist();
const char* get_title();
const char* get_album();
const char* get_genre();
unsigned int get_trackNumber();
unsigned int get_year();
void set_artist(const char*);
void set_title(const char*);
void set_album(const char*);
void set_genre(const char*);
void set_year(unsigned int);
void set_trackNumber(unsigned int);
};
#endif
Metadata.cpp:
#include <iostream>
#include "metadata.h"
Metadata::Metadata(const char* ar, const char* tit, const char* al) {
trackNumber = 0;
year = 0;
strncpy(genre, "unknown", sizeof(genre) -1);
genre[sizeof(genre)] = '\0';
strncpy(artist, ar, sizeof(artist) -1);
artist[sizeof(artist)] = '\0';
strncpy(title, tit, sizeof(title) -1);
title[sizeof(title)] = '\0';
strncpy(album, al, sizeof(album) -1);
album[sizeof(album)] = '\0';
}
const char* Metadata::get_artist() {
return artist;
}
const char* Metadata::get_title() {
return title;
}
const char* Metadata::get_album() {
return album;
}
const char* Metadata::get_genre() {
return genre;
}
void Metadata::set_artist(const char* ar) {
strncpy(artist, ar, sizeof(artist) -1);
artist[sizeof(artist)] = '\0';
}
void Metadata::set_title(const char* tit) {
strncpy(title, tit, sizeof(title) -1);
title[sizeof(title)] = '\0';
}
void Metadata::set_album(const char* al) {
strncpy(album, al, sizeof(album) -1);
album[sizeof(album)] = '\0';
}
void Metadata::set_genre(const char* g) {
strncpy(genre, g, sizeof(genre) -1);
genre[sizeof(genre)] = '\0';
}
void Metadata::set_trackNumber(unsigned int tn) {
trackNumber = tn;
}
void Metadata::set_year(unsigned int y) {
year = y;
}
For starters: All your string termination code is wrong in Metadata.cpp and Song.cpp:
Example:
strncpy(genre, "unknown", sizeof(genre) -1);
genre[sizeof(genre)] = '\0';
The call to strncpy() is correct. The hard-terminator is NOT. it should be:
strncpy(genre, "unknown", sizeof(genre) -1);
genre[sizeof(genre) -1] = '\0';
This is replicated on all your strings. recheck them all. it will surface several side-effects until you fix them. Considering these objects are all heap-allocated, you're corrupting your heap on the improper termination of Song::filename[] and MetaData::albumin[]. Don't just fix those two; fix them all.
Remember. sizeof() returns an octet-count of the field passed. which means char field[N]; will return N. if you already know C/C++ you know arrays are zero-based and therefore field[N-1] is the maximum allowable index.