Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,466 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,054 people online right now. Registration is fast and FREE... Join Now!




Class Composition + Memory Allocation

 
Reply to this topicStart new topic

Class Composition + Memory Allocation, am I doing it right.

#include<wmx010>
5 Feb, 2008 - 07:37 AM
Post #1

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
Hi.
I'm supposed to make up a program using composition and memory allocation.
The program works fine but am I doing it the right way ? I need your opinion.
Lots of files but they are short.

InventoryItem.h
CODE

#pragma once

class InventoryItem
{
public:
    InventoryItem();
    ~InventoryItem();

    void SetStockNumber(int stkNum);
    void SetPrice(double pr);
    void DisplayItem();

private:
    int    _stockNumber;
    double _price;
};



InventoryItem.cpp
CODE

#include "InventoryItem.h"
#include <iostream>
using namespace std;

InventoryItem::InventoryItem()
{
}

InventoryItem::~InventoryItem()
{
}

void InventoryItem::SetStockNumber(int stkNum)
{
    _stockNumber = stkNum;
}

void InventoryItem::SetPrice(double pr)
{
    _price = pr;
}

void InventoryItem::DisplayItem()
{
    cout << "Item #" << _stockNumber << " costs $" << _price << endl;
}


SalesPerson.h
CODE

#pragma once

class SalesPerson
{
public:
    SalesPerson() {}
    SalesPerson(const char* name);
    ~SalesPerson();

    void SetId(int id);
    void SetName(const char* name);
    void DisplayPerson();

private:
    int    _idNumber;
    char*  _empName;
};



salesPerson.cpp
CODE

#include "SalesPerson.h"
#include <iostream>
#include <cstring>
using namespace std;

SalesPerson::SalesPerson(const char* name)
{
    _empName = new char[strlen(name) + 1];
    strcpy(_empName, name);
}

SalesPerson::~SalesPerson()
{
    delete [] _empName;
}

void SalesPerson::SetId(int id)
{
    _idNumber = id;
}

void SalesPerson::SetName(const char* name)
{
    _empName = new char[strlen(name) + 1];
    strcpy(_empName, name);
}

void SalesPerson::DisplayPerson()
{
    cout << "Salesperson #" << _idNumber << " " << _empName << endl;
}



Transaction.h
CODE

#pragma once
#include "InventoryItem.h"
#include "Salesperson.h"

class Transaction
{
public:
    Transaction(int item, double pr, int salesId, const char* name);
    ~Transaction();

    void DisplayTransactionInfo();

private:
    InventoryItem _itemSold;
    SalesPerson   _seller;
};



transaction.cpp
CODE

#include "Transaction.h"

Transaction::Transaction(int item, double pr, int salesId, const char* name)
{
    _itemSold.SetPrice(pr);
    _itemSold.SetStockNumber(item);
    _seller.SetId(salesId);
    _seller.SetName(name);
}

Transaction::~Transaction()
{
}


void Transaction::DisplayTransactionInfo()
{
    _itemSold.DisplayItem();
    _seller.DisplayPerson();
}


main.cpp
CODE

#include "Transaction.h"
#include <conio.h>

int main ()
{
    Transaction oneSale(123, 139.95, 777, "Sherman");
    oneSale.DisplayTransactionInfo();

    _getch();
}


This post has been edited by #include<wmx010>: 5 Feb, 2008 - 07:41 AM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Class Composition + Memory Allocation
5 Feb, 2008 - 05:34 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
It's a lot of code, so it is very unlikely to get a reply if you ask the question like this, it's ok that you posted the code but you need to clarify, is it a review of the code you want, or a review of a small piece of the code? If the latter then also mark or post those lines. Just a hint.
User is offlineProfile CardPM
+Quote Post

#include<wmx010>
RE: Class Composition + Memory Allocation
5 Feb, 2008 - 05:58 PM
Post #3

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
It looks big because they are on separate files. But if you look closely you would notice that each file doesn't even have a scrolling so they are small. And yes I'm looking for a review of the code.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 02:13PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month