Total members 9950 | Gratitudes |It is currently Sat Feb 11, 2012 2:10 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 7 posts ] 
Author Question
 Question subject: Inheritance in c++
PostPosted: Sun Dec 07, 2008 5:43 pm 
Offline
Newbie
User avatar

Joined: Sun Dec 07, 2008 5:15 pm
Posts: 4
Has thanked: 0 time
Have thanks: 0 time

Write c++ program:-
The IT college
the steps:-
- The main class is named person , Two classes inheritant from the main class (Student ,Employee) .
-The members of the main class are :Identifier Id ,name ,Sex .
- The Member of the Student :student number , the Specialization of the student.
- The member of the class employee : employee number ,the work he do.
- Using the overload Function in output and input .
- Using the writeload Function in the print Function.
- Insert at lest 10 student and 10 employees .
- Print the list of the employees as a table with all own data.
- Print the list of the Students as a table with all own data.
- The proportion of male to female in the employee.
-After finish built a destructor function to destruct all the objects in the class.[/b]


Code:
#define male 'm'
#define female 'f'

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

class person
{
public:
    person(int,char,string);
    person();
    int id();
    char sex();
    string name();
    int changename(string);
    static int reset_count();
protected:
    int person_id;
    char person_sex;
    string person_name;
    static int count;
};

class student: public person
{
public:
    student (string,char);
    student (string,char,char);
    int number();
    char specialization();
protected:
    int student_number;
    char student_specialization;
};

class employee: public person
{
public:
    employee (int,char);
    employee (string,char,char);
    int number ();
    char working ();
protected:
    int employee_number;
    char employee_working;
};

int person::count;

int person::reset_count() {
    person::count=0;
    return (person::count);
}

person::person(int a, char b, string c)
{
    person_id=person::count;
    person::count++;
    person_sex=b;
    person_name=c;
}

person::person()
{
    person_id=person::count;
    person::count++;
    char this_persons_sex;
    int random=rand()%2;
    if (random==0) this_persons_sex='m';
    else this_persons_sex='f';
    person_sex=this_persons_sex;
    person_name="";
}


int person::id()
{
    return (person_id);
}

char person::sex()
{
    return (person_sex);
}

string person::name()
{
    return (person_name);
}

int person::changename(string a)
{
    person_name=a;
    return 0;
}


student::student(string a, char b )
{
    student_number=person_id;
    person_name=a;
    student_specialization=b;
}

student::student(string a, char b, char c )
{
    person_name=a;
    student_number=person_id;
    student_specialization=b;
    person_sex=c;
}

int student::number()
{
    return (student_number);
}

char student::specialization()
{
    return (student_specialization);
}

employee::employee(int a, char b )
{
    employee_number=a;
    employee_working=b;
}

employee::employee(string a, char b, char c )
{
    person_name=a;
    employee_number=person_id;
    person_sex=b;
    employee_working=c;
}

int employee::number()
{
    return (employee_number);
}

char employee::working()
{
    return (employee_working);
}

int main()
{
    person::reset_count();
    srand(time(NULL));
    employee ** emp;
    student ** stu;
    char tmpsex;
    string tmpname;
    string junk;
    char tmpspec;
    emp=new employee*[10];
    stu=new student*[10];

    cout<<"students:"<<endl;
    for (int i=0;i<10;i++)
    {
        cout<<"name: ";
        getline(cin,tmpname);
        cout<<"specialization: ";
        cin >> tmpspec;
        cout<<"sex: ";
        cin >> tmpsex;
        getline(cin,junk); // clear input buffer from junk cin leaves there
        stu[i]=new student(tmpname,tmpspec,tmpsex);
    }
    cout<<"employees:"<<endl;
    for (int i=0;i<10;i++)
    {
        cout<<"name: ";
        getline(cin,tmpname);
        cout<<"sex: ";
        cin>> tmpsex;
        cout << "Working?: ";
        cin >> tmpspec;
        getline(cin,junk);
        emp[i]=new employee(tmpname,tmpsex,tmpspec);
    }
    cout << "Student Data:\nid\tname\tsex\tspecialization\n";
    cout<<"-------------------------------------------------------------------------"<<endl;
    for (int i =0; i<10;++i)
    {
        cout<<stu[i]->id()<<"\t"<<stu[i]->name()<<"\t"<<stu[i]->sex()<<"\t"<<stu[i]->specialization()<<endl;
    }
    cout<<"-------------------------------------------------------------------------"<<endl;
    cout << "\nEmployee Data:\nid\tname\tsex\tworking\n";
    cout<<"-------------------------------------------------------------------------"<<endl;
    for (int i =0; i<10;++i)
    {
        cout<<emp[i]->id()<<"\t"<<emp[i]->name()<<"\t"<<emp[i]->sex()<<"\t"<<emp[i]->working()<<endl;
    }
    cout<<"-------------------------------------------------------------------------"<<endl;
    cout<<"\n\nPress <ENTER> to Exit. ";
    cin.get();
    return 0; }


I can write the above program but I can not solve the last step:-

-After finish built a destructor function to destruct all the objects in the class.[/b]

please help me to complete the program in C++[/u]


thanks :oops:


Last edited by msi_333 on Sun Dec 07, 2008 8:57 pm, edited 1 time in total.
make look it better :)


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Inheritance in c++
PostPosted: Sun Dec 07, 2008 8:53 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time
to make a destructor in C++

use the character ~ you define destructor
Destructors are implicitly called when an automatic object (a local object that has been declared auto or register, or not declared as static or extern) or temporary object passes out of scope. They are implicitly called at program termination for constructed external and static objects. Destructors are invoked when you use the delete operator for objects created with the new operator.

Code:
#include <string>

class Y {
private:
  char * string;
  int number;
public:
  // Constructor
  Y(const char*, int);
  // Destructor
  ~Y() { delete[] string; }
};

// Define class Y constructor
Y::Y(const char* n, int a) {
  string = strcpy(new char[strlen(n) + 1 ], n);
  number = a;
}

int main () {
  // Create and initialize
  // object of class Y
  Y yobj = Y("somestring", 10);

  // ...

  // Destructor ~Y is called before
  // control returns from main()
}

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Inheritance in c++
PostPosted: Mon Dec 08, 2008 1:20 pm 
Offline
Newbie
User avatar

Joined: Sun Dec 07, 2008 5:15 pm
Posts: 4
Has thanked: 0 time
Have thanks: 0 time
thank you sir

Code:
#include <string>

class person {
private:
   person(int,char,string);
    person();
    int id();
    char sex();
    string name();
    int changename(string);
    static int reset_count();
public:
  // Constructor
  int person_id;
    char person_sex;
    string person_name;
    static int count;
  // Destructor

  ~person() { delete[] string; }
};

// Define class person constructor
person::~person(int a, char b, string c)
{
    person_id=person::count;
    person::count++;
    person_sex=b;
    person_name=c;
}

person::~person()
{
    person_id=person::count;
    person::count++;
    char this_persons_sex;
    int random=rand()%2;
    if (random==0) this_persons_sex='m';
    else this_persons_sex='f';
    person_sex=this_persons_sex;
    person_name="";
}


int person::id()
{
    return (person_id);
}

char person::sex()
{
    return (person_sex);
}

string person::name()
{
    return (person_name);
}

int person::changename(string a)
{
    person_name=a;
    return 0;
}

class student: public person
{
public:
    student (string,char);
    student (string,char,char);
    int number();
    char specialization();
private:
    int student_number;
    char student_specialization;
};

~student() { delete[] string; }
};
student::~student(string a, char b )
{
    student_number=person_id;
    person_name=a;
    student_specialization=b;
}

student::~student(string a, char b, char c )
{
    person_name=a;
    student_number=person_id;
    student_specialization=b;
    person_sex=c;
}

int student::number()
{
    return (student_number);
}

char student::specialization()
{
    return (student_specialization);
}

employee::employee(int a, char b )
{
    employee_number=a;
    employee_working=b;
}

class employee: public person
{
public:
    employee (int,char);
    employee (string,char,char);
    int number ();
    char working ();
private:
    int employee_number;
    char employee_working;
};

int person::count;

int person::reset_count() {
    person::count=0;
    return (person::count);
}

  ~employee() { delete[] string; }

employee::~employee(int a, char b )
{
    employee_number=a;
    employee_working=b;
}

employee::~employee(string a, char b, char c )
{
    person_name=a;
    employee_number=person_id;
    person_sex=b;
    employee_working=c;
}

int employee::number()
{
    return (employee_number);
}

char employee::working()
{
    return (employee_working);
}

int main () {
Y yobj = Y("somestring", 10);
person::reset_count();
    srand(time(NULL));
    employee ** emp;
    student ** stu;
    char tmpsex;
    string tmpname;
    string junk;
    char tmpspec;
    emp=new employee*[10];
    stu=new student*[10];

    cout<<"students:"<<endl;
    for (int i=0;i<10;i++)
    {
        cout<<"name: ";
        getline(cin,tmpname);
        cout<<"specialization: ";
        cin >> tmpspec;
        cout<<"sex: ";
        cin >> tmpsex;
        getline(cin,junk); // clear input buffer from junk cin leaves there
        stu[i]=new student(tmpname,tmpspec,tmpsex);
    }
    cout<<"employees:"<<endl;
    for (int i=0;i<10;i++)
    {
        cout<<"name: ";
        getline(cin,tmpname);
        cout<<"sex: ";
        cin>> tmpsex;
        cout << "Working?: ";
        cin >> tmpspec;
        getline(cin,junk);
        emp[i]=new employee(tmpname,tmpsex,tmpspec);
    }
    cout << "Student Data:\nid\tname\tsex\tspecialization\n";
    cout<<"-------------------------------------------------------------------------"<<endl;
    for (int i =0; i<10;++i)
    {
        cout<<stu[i]->id()<<"\t"<<stu[i]->name()<<"\t"<<stu[i]->sex()<<"\t"<<stu[i]->specialization()<<endl;
    }
    cout<<"-------------------------------------------------------------------------"<<endl;
    cout << "\nEmployee Data:\nid\tname\tsex\tworking\n";
    cout<<"-------------------------------------------------------------------------"<<endl;
    for (int i =0; i<10;++i)
    {
        cout<<emp[i]->id()<<"\t"<<emp[i]->name()<<"\t"<<emp[i]->sex()<<"\t"<<emp[i]->working()<<endl;
    }
    cout<<"-------------------------------------------------------------------------"<<endl;
    cout<<"\n\nPress <ENTER> to Exit. ";
    cin.get();
    return 0;
  // control returns from main()
}



Pleeeeeeeeeeeeeeez correct the mistakes


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Inheritance in c++
PostPosted: Mon Dec 08, 2008 1:50 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time
Here you are implementing constructors
Code:
  ~employee() { delete[] string; }

employee::~employee(int a, char b )
{
    employee_number=a;
    employee_working=b;
}

employee::~employee(string a, char b, char c )
{
    person_name=a;
    employee_number=person_id;
    person_sex=b;
    employee_working=c;
}


But here , you defined a a constructors , i think you should do ~ here too .

Code:
class employee: public person
{
public:
    employee (int,char);
    employee (string,char,char);
    int number ();
    char working ();
protected:
    int employee_number;
    char employee_working;
};

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Inheritance in c++
PostPosted: Mon Dec 08, 2008 4:41 pm 
Offline
Newbie
User avatar

Joined: Sun Dec 07, 2008 5:15 pm
Posts: 4
Has thanked: 0 time
Have thanks: 0 time
thank you mr.msi_333 for your efforts..

Only,Is this the mistak on the program??

pleeeez answer to me ...

Is the whole program correctly??

pleeeeez correct any mistak in it

I need this program very very important for me.

best wishes..IT STUDENT


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Inheritance in c++
PostPosted: Tue Dec 09, 2008 7:47 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time
yes it is a mistake ,, Did u read my older reply.

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Inheritance in c++
PostPosted: Wed Dec 10, 2008 5:06 am 
Offline
Newbie
User avatar

Joined: Sun Dec 07, 2008 5:15 pm
Posts: 4
Has thanked: 0 time
Have thanks: 0 time
Code:
#define male 'm'
#define female 'f'

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

class person
{
public:
    person(int,char,string);
    person();
    int id();
    char sex();
    string name();
    int changename(string);
    static int reset_count();
protected:
    int person_id;
    char person_sex;
    string person_name;
    static int count;
};

class student: public person
{
public:
    student (string,char);
    student (string,char,char);
    int number();
    char specialization();
protected:
    int student_number;
    char student_specialization;
};

class employee: public person
{
public:
    employee (int,char);
    employee (string,char,char);
    int number ();
    char working ();
protected:
    int employee_number;
    char employee_working;
};

int person::count;

int person::reset_count() {
    person::count=0;
    return (person::count);
}

person::person(int a, char b, string c)
{
    person_id=person::count;
    person::count++;
    person_sex=b;
    person_name=c;
}

person::person()
{
    person_id=person::count;
    person::count++;
    char this_persons_sex;
    int random=rand()%2;
    if (random==0) this_persons_sex='m';
    else this_persons_sex='f';
    person_sex=this_persons_sex;
    person_name="";
}


int person::id()
{
    return (person_id);
}

char person::sex()
{
    return (person_sex);
}

string person::name()
{
    return (person_name);
}

int person::changename(string a)
{
    person_name=a;
    return 0;
}


student::student(string a, char b )
{
    student_number=person_id;
    person_name=a;
    student_specialization=b;
}

student::student(string a, char b, char c )
{
    person_name=a;
    student_number=person_id;
    student_specialization=b;
    person_sex=c;
}

int student::number()
{
    return (student_number);
}

char student::specialization()
{
    return (student_specialization);
}

employee::employee(int a, char b )
{
    employee_number=a;
    employee_working=b;
}

employee::employee(string a, char b, char c )
{
    person_name=a;
    employee_number=person_id;
    person_sex=b;
    employee_working=c;
}

int employee::number()
{
    return (employee_number);
}

char employee::working()
{
    return (employee_working);
}

int main()
{
    person::reset_count();
    srand(time(NULL));
    employee ** emp;
    student ** stu;
    char tmpsex;
    string tmpname;
    string junk;
    char tmpspec;
    emp=new employee*[10];
    stu=new student*[10];

    cout<<"students:"<<endl;
    for (int i=0;i<10;i++)
    {
        cout<<"name: ";
        getline(cin,tmpname);
        cout<<"specialization: ";
        cin >> tmpspec;
        cout<<"sex: ";
        cin >> tmpsex;
        getline(cin,junk); // clear input buffer from junk cin leaves there
        stu[i]=new student(tmpname,tmpspec,tmpsex);
    }
    cout<<"employees:"<<endl;
    for (int i=0;i<10;i++)
    {
        cout<<"name: ";
        getline(cin,tmpname);
        cout<<"sex: ";
        cin>> tmpsex;
        cout << "Working?: ";
        cin >> tmpspec;
        getline(cin,junk);
        emp[i]=new employee(tmpname,tmpsex,tmpspec);
    }
    cout << "Student Data:\nid\tname\tsex\tspecialization\n";
    cout<<"-------------------------------------------------------------------------"<<endl;
    for (int i =0; i<10;++i)
    {
        cout<<stu[i]->id()<<"\t"<<stu[i]->name()<<"\t"<<stu[i]->sex()<<"\t"<<stu[i]->specialization()<<endl;
    }
    cout<<"-------------------------------------------------------------------------"<<endl;
    cout << "\nEmployee Data:\nid\tname\tsex\tworking\n";
    cout<<"-------------------------------------------------------------------------"<<endl;
    for (int i =0; i<10;++i)
    {
        cout<<emp[i]->id()<<"\t"<<emp[i]->name()<<"\t"<<emp[i]->sex()<<"\t"<<emp[i]->working()<<endl;
    }
    cout<<"-------------------------------------------------------------------------"<<endl;
    cout<<"\n\nPress <ENTER> to Exit. ";
    cin.get();
    return 0; }
#include <string>

class person {
private:
   person(int,char,string);
    person();
    int id();
    char sex();
    string name();
    int changename(string);
    static int reset_count();
public:
  // Constructor
  int person_id;
    char person_sex;
    string person_name;
    static int count;
  // Destructor

  ~person() { delete[] string; }
};

// Define class person constructor
person::~person(int a, char b, string c)
{
    person_id=person::count;
    person::count++;
    person_sex=b;
    person_name=c;
}

person::~person()
{
    person_id=person::count;
    person::count++;
    char this_persons_sex;
    int random=rand()%2;
    if (random==0) this_persons_sex='m';
    else this_persons_sex='f';
    person_sex=this_persons_sex;
    person_name="";
}


int person::id()
{
    return (person_id);
}

char person::sex()
{
    return (person_sex);
}

string person::name()
{
    return (person_name);
}

int person::changename(string a)
{
    person_name=a;
    return 0;
}

class student: public person
{
public:
    student (string,char);
    student (string,char,char);
    int number();
    char specialization();
private:
    int student_number;
    char student_specialization;
};

~student() { delete[] string; }
};
student::~student(string a, char b )
{
    student_number=person_id;
    person_name=a;
    student_specialization=b;
}

student::~student(string a, char b, char c )
{
    person_name=a;
    student_number=person_id;
    student_specialization=b;
    person_sex=c;
}

int student::number()
{
    return (student_number);
}

char student::specialization()
{
    return (student_specialization);
}

employee::employee(int a, char b )
{
    employee_number=a;
    employee_working=b;
}

class employee:~ public person
{
public:
    employee (int,char);
    employee (string,char,char);
    int number ();
    char working ();
private:
    int employee_number;
    char employee_working;
};

int person::count;

int person::reset_count() {
    person::count=0;
    return (person::count);
}

  ~employee() { delete[] string; }

employee::~employee(int a, char b )
{
    employee_number=a;
    employee_working=b;
}

employee::~employee(string a, char b, char c )
{
    person_name=a;
    employee_number=person_id;
    person_sex=b;
    employee_working=c;
}

int employee::number()
{
    return (employee_number);
}

char employee::working()
{
    return (employee_working);
}

int main () {
Y yobj = Y("somestring", 10);
person::reset_count();
    srand(time(NULL));
    employee ** emp;
    student ** stu;
    char tmpsex;
    string tmpname;
    string junk;
    char tmpspec;
    emp=new employee*[10];
    stu=new student*[10];

    cout<<"students:"<<endl;
    for (int i=0;i<10;i++)
    {
        cout<<"name: ";
        getline(cin,tmpname);
        cout<<"specialization: ";
        cin >> tmpspec;
        cout<<"sex: ";
        cin >> tmpsex;
        getline(cin,junk); // clear input buffer from junk cin leaves there
        stu[i]=new student(tmpname,tmpspec,tmpsex);
    }
    cout<<"employees:"<<endl;
    for (int i=0;i<10;i++)
    {
        cout<<"name: ";
        getline(cin,tmpname);
        cout<<"sex: ";
        cin>> tmpsex;
        cout << "Working?: ";
        cin >> tmpspec;
        getline(cin,junk);
        emp[i]=new employee(tmpname,tmpsex,tmpspec);
    }
    cout << "Student Data:\nid\tname\tsex\tspecialization\n";
    cout<<"-------------------------------------------------------------------------"<<endl;
    for (int i =0; i<10;++i)
    {
        cout<<stu[i]->id()<<"\t"<<stu[i]->name()<<"\t"<<stu[i]->sex()<<"\t"<<stu[i]->specialization()<<endl;
    }
    cout<<"-------------------------------------------------------------------------"<<endl;
    cout << "\nEmployee Data:\nid\tname\tsex\tworking\n";
    cout<<"-------------------------------------------------------------------------"<<endl;
    for (int i =0; i<10;++i)
    {
        cout<<emp[i]->id()<<"\t"<<emp[i]->name()<<"\t"<<emp[i]->sex()<<"\t"<<emp[i]->working()<<endl;
    }
    cout<<"-------------------------------------------------------------------------"<<endl;
    cout<<"\n\nPress <ENTER> to Exit. ";
    cin.get();
    return 0;
  // control returns from main()
}



the whole of program of constructor and Destructor

IS WRITING of the program CORRECT or not??

and very very thanks

thank you very much Mr.msi_333


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 7 posts ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 help me! inheritance  Java  Nasr  6
 @AttributeOverride with Inheritance  JPA  msi_333  0
 single table inheritance  JPA  msi_333  0
 Inheritance in java  Java  msi_333  0
 Need Help about Inheritance  Java  Dummies In Java  1

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team