[Solved] Having a Private and Public Overriding Function

I want to do a overloading private fucntion to see it work, didnt have any compile error, it said a weird answer on the console. Do you know the reason it might be.

Using Software DevC++

# include <iostream>
# include <iomanip>
using namespace std;

class data
{
private:
string name,title,occup;
int id;
public:
data()
{
name="Tom",title="The Head",occup="The king";
id=0;
}
data(string cname, string ctitle, string coccup, int cid )
{
name=cname,title=ctitle,occup=coccup, id=cid;
}
data(string ccname, string cctitle)
{
name=ccname;
title=cctitle;
}
data(string cccname)
{
name=cccname;
}
void sname(string);
void stitle(string);
void soccup(string);
void sid(int);
string gname();
string gtitle();
string goccup();
int gid();
};
void data::sname(string fname)
{
name=fname;
}

void data::stitle(string ftitle)
{
title=ftitle;
}

void data::soccup(string foccup)
{
occup=foccup;
}
void data::sid(int fid)
{
id=fid;
}

string data::gname()
{
return name;
}
string data:: gtitle()
{
return title;
}

string data::goccup()
{
return occup;
}

int data:: gid()
{
return id;
}

int main()
{
const int size=3;
string thename[size];
string thetitle[size];
string theoccup[size];
int theid[size];


for(int a=0; a<size; a++)
{
cin>>thename[a]>>thetitle[a]>>theoccup[a]>>theid[a];
}

data obj1;
obj1.sname(thename[0]);
obj1.stitle(thetitle[0]);
obj1.soccup(theoccup[0]);
obj1.sid(theid[0]);
data obj2(thename[1],thetitle[1],theoccup[1],theid[1]);
data obj3( thename[2],thetitle[2]);
obj3.soccup(theoccup[2]);
obj3.sid(theid[2]);
data obj4(thename[3]);
obj4.stitle(thetitle[3]);
obj4.soccup(theoccup[3]);
obj4.sid(theid[3]);



cout<<"Name"<<setw(20)<<"ID Number"<<setw(20)<<" Department"<<setw(20)<<"Position"<<endl<<endl;
cout<<obj1.gname()<<setw(9)<<obj1.gid()<<setw(25)<<obj1.gtitle()<<setw(25)<<obj1.goccup()<<endl<<endl;
cout<<obj2.gname()<<setw(9)<<obj2.gid()<<setw(25)<<obj2.gtitle()<<setw(25)<<obj2.goccup()<<endl<<endl;
cout<<obj3.gname()<<setw(9)<<obj3.gid()<<setw(25)<<obj3.gtitle()<<setw(25)<<obj3.goccup()<<endl<<endl;

return 0;
}










T
I INPUT THIS
Chris
King
MRKING
8008
NEO
MasterNEo
Theone
7007
Gon
ROck
paper
9000

AND IT OUT PUT THIS
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

--------------------------------
Process exited after 84.74 seconds with return value 3
Press any key to continue . . .

Last edited on
SOLVE I made 3 array, i made a 4th array for object4 which i did not set for the size;


TY GUYS
I would recommend changing IDEs to Code::Blocks and trying again because I didn't get any errors when compiling, building, and running your code. Though I don't know what the output is supposed to be so there isn't much I can comment beyond maybe you're using an outdated IDE/Compiler. Get Code::Blocks and try the code again, then post any errors here.
Last edited on
THX for that reply , i wanted assign values from the private classes but it work. when i wrote
data obj4(thename[3]);
obj4.stitle(thetitle[3]);
obj4.soccup(theoccup[3]);
obj4.sid(theid[3]);
should of been 2 instead of three, works now. thx for the help. love you guys.

# include <iostream>
# include <iomanip>
using namespace std;

class data
{
private:
string name,title,occup;
int id;
public:
data()
{
name="Tom",title="The Head",occup="The king";
id=0;
}
data(string cname, string ctitle, string coccup, int cid )
{
name=cname,title=ctitle,occup=coccup, id=cid;
}
data(string ccname, string cctitle)
{
name=ccname;
title=cctitle;
}
data(string cccname)
{
name=cccname;
}

void sname(string fname)
{
name=fname;
}

void stitle(string ftitle)
{
title=ftitle;
}

void soccup(string foccup)
{
occup=foccup;
}
void sid(int fid)
{
id=fid;
}

string gname()
{
return name;
}
string gtitle()
{
return title;
}

string goccup()
{
return occup;
}

int gid()
{
return id;
}

};
int main()
{
const int size=3;
string thename[size];
string thetitle[size];
string theoccup[size];
int theid[size];


for(int a=0; a<size; a++)
{
cin>>thename[a]>>thetitle[a]>>theoccup[a]>>theid[a];
}

data obj1;
obj1.sname(thename[0]);
obj1.stitle(thetitle[0]);
obj1.soccup(theoccup[0]);
obj1.sid(theid[0]);
data obj2(thename[1],thetitle[1],theoccup[1],theid[1]);
data obj3( thename[2],thetitle[2]);
obj3.soccup(theoccup[2]);
obj3.sid(theid[2]);
/* data obj4(thename[3]);
obj4.stitle(thetitle[3]);
obj4.soccup(theoccup[3]);
obj4.sid(theid[3]);*/



cout<<"Name"<<setw(20)<<"ID Number"<<setw(20)<<" Department"<<setw(20)<<"Position"<<endl<<endl;
cout<<obj1.gname()<<setw(9)<<obj1.gid()<<setw(25)<<obj1.gtitle()<<setw(25)<<obj1.goccup()<<endl<<endl;
cout<<obj2.gname()<<setw(9)<<obj2.gid()<<setw(25)<<obj2.gtitle()<<setw(25)<<obj2.goccup()<<endl<<endl;
cout<<obj3.gname()<<setw(9)<<obj3.gid()<<setw(25)<<obj3.gtitle()<<setw(25)<<obj3.goccup()<<endl<<endl;

return 0;
}


Topic archived. No new replies allowed.