Skip to content

Instantly share code, notes, and snippets.

@Alejandro131
Created March 18, 2014 18:29
Show Gist options
  • Save Alejandro131/9626361 to your computer and use it in GitHub Desktop.
Save Alejandro131/9626361 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string.h>
using namespace std;
class Label
{
public:
Label(const char* = "", int = 0);
Label(const Label& other);
Label& operator = (const Label& other);
~Label();
void setBarcode(int b);
int getBarcode() const;
void setContent(const char* c);
char* getContent() const;
private:
void copyLabel(const Label& other);
void deleteLabel();
int barcode;
char* content;
};
Label::Label(const char* _content, int _barcode)
{
content = new char[strlen(_content) + 1];
strcpy(content, _content);
barcode = _barcode;
}
Label::Label(const Label& other)
{
copyLabel(other);
}
Label& Label::operator = (const Label& other)
{
if (this != &other)
{
deleteLabel();
copyLabel(other);
}
return *this;
}
Label::~Label()
{
deleteLabel();
}
void Label::setBarcode(int b)
{
barcode = b;
}
int Label::getBarcode() const
{
return barcode;
}
void Label::setContent(const char* c)
{
delete[] content;
content = new char[strlen(c) + 1];
strcpy(content, c);
}
char* Label::getContent() const
{
return content;
}
void Label::copyLabel(const Label& other)
{
content = new char[strlen(other.content) + 1];
strcpy(content, other.content);
barcode = other.barcode;
}
void Label::deleteLabel()
{
delete[] content;
}
int main()
{
char ala[20] = "321321";
Label l("dsahgdasgi");
cout << l.getContent() << " " << l.getBarcode() << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment