Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Inheritance problem!!!

vishwadevvishwadev Member Posts: 4
I have a little problem here maybe someone could help me please. I have to provide a Programmer class which is a sub class of both Person class and the Staff class. I have done simple inheritance but I dont know much about multiple inheritance. Can anyone please help me with the assignment operators and their implementation. Here are the Person.h and Staff.h file:

Person.h

#ifndef __PERSON_H
#define__PERSON_H

class Person{
Protected:
char* name;
int age;

Public:
Person(char*, int);
Person(const Person&);
Person& operator=(const Person&); //???
~Person();
char* getName();
int getAge();
};
#endif

&&&&&&&&&&&&&

Staff.h

#ifndef __STAFF_H
#define__STAFF_H
class Staff{
Protected :
Char* post;

Public:
Staff(char*);
Staff(const Staff&);
Staffoperator=(const Staff&); //???
`Staff();
char* getPost();
};
#endif

Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    Here is a hint to get you started. Note the checks for NULL to prevent your program from crashing.

    [code]
    class Person{
    protected:
    char* name;
    int age;

    public:
    Person(char* nm, int a)
    {
    if(ns != NULL)
    {
    name = new char[strlen(nm)+1];
    strcpy(name,nm);
    }
    else
    name = NULL;
    age = a;
    }
    Person(const Person&);
    Person& operator=(const Person& psn) //???
    {
    if(name != NULL)
    {
    delete[] name;
    name = NULL;
    }
    if(psn.name != NULL)
    {
    name = new char[strlen(psn.name)+1];
    strcpy(name,psn.name);
    }
    age = psn.age;
    }
    ~Person();
    char* getName();
    int getAge();
    };
    [/code]

  • vishwadevvishwadev Member Posts: 4
    Hi
    Thanks for the reply, it is very helpful. Thanks


  • Shawn CarterShawn Carter Member Posts: 0

    ( http://forcoder.org ) free ebooks and video tutorials about < Scratch, Perl, Java, Swift, Delphi, PL/SQL, C, C++, Go, Visual Basic, JavaScript, R, Assembly, Objective-C, PHP, Python, Visual Basic .NET, MATLAB, C#, Ruby Apex, Ada, Julia, Bash, Lua, LabVIEW, Fortran, SAS, Lisp, Clojure, Prolog, Crystal, ABAP, Dart, Awk, COBOL, Kotlin, Alice, FoxPro, Erlang, VBScript, ML, Rust, Scala, F#, Transact-SQL, Hack, D, Logo, Scheme /> _

Sign In or Register to comment.