Q: Class + pointer

This is a discussion on Q: Class + pointer within the C++ Programming forums, part of the General Programming Boards category; Hello, I have tried to solve an exercise, because i want to make sure if i've understand what i've learned. ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Question Q: Class + pointer

    Hello,
    I have tried to solve an exercise, because i want to make sure if i've understand what i've learned.
    I hope that you can let me know if thier is any mistakes, and i will try to solve it.

    Question 1
    Consider the following definition of the class Employee:
    Code:
    class Employee
    { private:
    string * firstName;
    string * lastName;
    static int empCount;
    public:
    Employee(string first, string last);
    ~Employee();
    string getFirstName();
    static int getEmpCount();
    static int counting;
    };
    PART (A)
    Write the definition of the constructor function Employee(string first, string
    last); which dynamically allocates memory for firstName and lastName and copies first
    and last to them. The function should increment empCount by 1. Use deep copy.
    Code:
    Employee :: Employee(string first, string last)
    {
    first=new string;
    last=new string;
    first=*firstname;
    last=*lastname;
    empCount++;
    }
    PART (B)
    Write the definition of the function ~Employee() which deallocates the dynamic memory and
    decrease the value of empCount by 1.
    Code:
    Employee :: ~ Employee()
    {
    delete firstname;
    delete lastname;
    empCount--;
    }
    PART (C)
    Write the definition the function getEmpCount() which returns the value of empCount.
    Code:
    int Employee:: getempCount()
    {
    return empCount;
    }
    PART (D)
    In the main function add statements to do the following:
    Using the following declarations: string first=”Hilary”, last=”Sam”, create an object of
    type class Employee named xEmp.
    Code:
    Employee xEmp("Hilary","Sam");
    Increment the value of counting in xEmp by 1.
    Code:
    xEmp.counting++;
    Print the value of empCount.
    Code:
    cout<<xEmp.getempCount;
    Last edited by princess; 06-11-2010 at 07:17 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Well, Part B,C are definitely correct.
    I am concerned about part D2) Increment the value of counting. If it's just supposed to increment the variable counting inside the class, then it's right.
    D3 isn't right since you've misspelled GetEmpCount.

    Btw, your indentation could use some work.
    Last edited by Elysia; 06-10-2010 at 02:25 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    Employee :: Employee(string first, string last)
    {
    first=new string;
    last=new string;
    first=*firstname;
    last=*lastname;
    empCount++;
    }
    You've mixed up your class members with the parameters there.

    Also, someone should point out to your teacher that those parameters should be const references rather than temporaries, and that using new/delete without RAII is just asking for trouble...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    thank you very much for your help

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    Hi again,

    what about D1, is it right ?

    part A
    Code:
    Employee :: Employee(const string &first, const string &last)
    {
    first=new string;
    last=new string;
    firstname=first.firstname;
    lastname=last.lastname;
    empCount++;
    }
    so, is it right now ?!

  6. #6
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by princess View Post
    Hi again,

    what about D1, is it right ?

    part A
    Code:
    Employee :: Employee(const string &first, const string &last)
    {
    first=new string;
    last=new string;
    firstname=first.firstname;
    lastname=last.lastname;
    empCount++;
    }
    so, is it right now ?!
    No...
    You need to change it to:
    Code:
    Employee :: Employee(const string &first, const string &last)
    {
    firstname=new string;
    lastname=new string;
    *firstname = first;
    *lastname=last;
    empCount = 0;
    empCount++;
    }
    Last edited by Programmer_P; 06-11-2010 at 07:45 AM. Reason: fixed typo

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    thank you very much

    what about D1?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    You mean
    Code:
    Employee xEmp("Hilary","Sam");
    ?
    I think they want you to create two string objects first and then create the employee object. In that case, you're not doing it right (but it works).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21