You are on page 1of 5

Class Exercise 3: C'tors, D'tors

Object-Oriented Programming (83-131), 5771

1) Another classes example


MyTime.h
class MyTime { private: int m_hour; int m_min; public: // Set the time to zero void init() { m_hour = 0; m_min = 0; } // Set the time void set(int hour, int min); void set(const char* time_str); void set_now(); // Get functions const int& getHour() { return m_hour; } const int& getMin() { return m_min; } // Print the time void print(); private: // Check that a given value is in range bool isHour(int val) { return (val >= 0 && val <= 23); } bool isMin(int val) { return (val >= 0 && val <= 59); } }; val[0] = time_str[3]; val[1] = time_str[4]; min = atol(val); set(hour, min); } } void MyTime::setNow() { time_t rawtime; struct tm* timeinfo; time(&rawtime); timeinfo = localtime(&rawtime); m_hour = timeinfo->tm_hour; m_min = timeinfo->tm_min; }

MyTime.cpp
#include <iostream> #include <time.h> #include "MyTime.h" using namespace std; void MyTime::set(int hour, int min) { if (isHour(hour) && isMin(min)) { m_hour = hour; m_min = min; } else { cout << "ERROR" << endl; } } void MyTime::set(const char* time_str) { if (strlen(time_str) != 5) { cout << "ERROR" << endl; } else { int hour, min; char val[3]; val[2] = '\0'; val[0] = time_str[0]; val[1] = time_str[1]; hour = atol(val);

void MyTime::print() { cout << m_hour << ":" << m_min << endl; }

Class Exercise 3: C'tors, D'tors

Object-Oriented Programming (83-131), 5771

MyDateTime.h
#include "MyDate.h" #include "MyTime.h" class MyDateTime { private: MyDate m_date; MyTime m_time; public: // Set the time to zero void init(); // Set the date void set( const MyDate& date, const MyTime& time); // Print the time void print(); MyDate& getDate() { return m_date; } MyTime& getTime() { return m_time; } void copy_to(MyDateTime& other); };

MyDateTime.cpp
#include <iostream> #include "MyDateTime.h" using namespace std; void MyDateTime::init() { m_date.init(); m_time.init(); } void MyDateTime::set( const MyDate& date, const MyTime& time) { m_date = date; m_time = time; } void MyDateTime::print() { m_date.print(); cout << " "; m_time.print(); } void MyDateTime::copy_to(MyDateTime& other) { other.m_date = m_date; other.m_time = m_time; }

MyDate.h
class MyDate { private: int m_day; int m_month; int m_year; public: // Initialize the date void init(); // Set the coordinates of the point void set(int day, int month, int year); // Print the date void print(); }; }

main.cpp
int main() { MyDate d1; d1.init(); d1.set(5, 11, 2009); MyTime t1; t1.init(); t1.set(10, 13); MyDateTime dt1; dt1.init(); dt1.set(d1, t1); dt1.print(); cout << endl; system("PAUSE"); return 0;

Class Exercise 3: C'tors, D'tors

Object-Oriented Programming (83-131), 5771

DateList.h
#include "MyDateTime.h" class DateList { private: MyDateTime* vector; int num_items; public: void init() {vector=NULL; num_items=0;} int get_num_items() { return num_items; } void add_item(const MyDateTime& item); bool is_empty() { return !(bool)num_items; } } void empty_list(); void print(); MyDateTime& get_item(int index) { return vector[index]; } }; }

DateList.cpp
#include <iostream> #include "DateList.h" using namespace std; void DateList::add_item(const MyDateTime& item) { MyDateTime* help = new MyDateTime[num_items+1]; for (int i = 0; i < num_items; i++) vector[i].copy_to(help[i]); delete[] vector; vector = help; item.copy_to(vector[num_items]); num_items++;

void DateList::empty_list() { delete[] vector; vector = NULL; num_items = 0;

void DateList::print() { // ... }

main.cpp
int main() { MyDate d1; d1.set(5, 11, 2009); MyTime t1; t1.set(10, 13); MyDateTime dt1; dt1.set(d1, t1); DateList list; list.init(); list.add_item(dt1); list.print(); dt1.getTime().set(11, 40); list.add_item(dt1); list.print();

// "5/11/2009 10:13"

// "5/11/2009 10:13, 5/11/2009 11:40"

list.get_item(0).getTime().set(15, 17); list.print(); // "5/11/2009 15:17, 5/11/2009 11:40" list.empty_list(); system("PAUSE"); return 0; }

Class Exercise 3: C'tors, D'tors

Object-Oriented Programming (83-131), 5771

2) Constructors and destructors


MyString.h
class MyString { private: char* m_str; public: MyString(); MyString(const MyString& other); MyString(const char* str); ~MyString(); void void bool bool copyFrom(const char* str); copyFrom(const MyString& other); isEqual(const char* str) const; isEqual(const MyString& other);

MyString.cpp
MyString::MyString() : m_str(NULL) { m_str = new char[1]; m_str[0] = '\0'; } MyString::MyString(const MyString& other) : m_str(NULL) { copyFrom(other); } MyString::MyString(const char* str) : m_str(NULL) { copyFrom(str); } MyString::~MyString() { delete[] m_str; } void MyString::copyFrom(const char* str) { delete[] m_str; m_str = new char[strlen(str) + 1]; strcpy(m_str, str); } void MyString::copyFrom(const MyString& other) { copyFrom(other.m_str); } bool MyString::isEqual(const char* str) const { return (strcmp(m_str, str) == 0); } bool MyString::isEqual(const MyString& other) { return (strcmp(m_str, other.m_str) == 0); } char& MyString::getChar(unsigned int n) { if (n > strlen(m_str)) { cout << "Out Of Range" << endl; return (m_str[0]); } return (m_str[n]); } MyString& MyString::toUpper() { for (int i = 0; i < strlen(m_str); ++i) { if (m_str[i] >= 'a' && m_str[i] <= 'z') m_str[i] = m_str[i] + ('A' - 'a'); } return (*this); }

char& getChar(unsigned int n); void print() { cout << m_str << endl; } MyString& toUpper(); };

main.cpp
int main() { MyString s1; s1.copyFrom("Shalom"); s1.print(); MyString s2(s1); s2.print(); if (s1.isEqual(s2)) cout << "Equal" << endl; s2.toUpper().print(); s2.print(); return 0; }

Class Exercise 3: C'tors, D'tors

Object-Oriented Programming (83-131), 5771

Student.h
class Student { private: MyString m_name; MyString* m_hobbies; int m_hobbies_count; public: Student() : m_hobbies(NULL), m_hobbies_count(0) { } Student(const MyString& name) : m_name(name), m_hobbies(NULL), m_hobbies_count(0) { } ~Student() { delete[] m_hobbies; } void addHobbie(const char* s); void print(); }; }

Student.cpp
void Student::addHobbie(const char* s) { MyString* temp = new MyString[m_hobbies_count + 1]; for (int i = 0; i < m_hobbies_count; ++i) temp[i].copyFrom(m_hobbies[i]); temp[m_hobbies_count].copyFrom(s); delete[] m_hobbies; m_hobbies = temp; ++m_hobbies_count; } void Student::print() { m_name.print(); for (int i = 0; i < m_hobbies_count; ++i) m_hobbies[i].print();

main.cpp
int main() { Student std1(MyString("Baruch")); std1.addHobbie("Fortran"); std1.addHobbie("Pascal"); std1.print(); return 0; }

You might also like