Multiple Inheritance in Object-Oriented Programming using C++

multiple inheritance n.w
1 / 9
Embed
Share

"Explore multiple inheritance in C++ along with Date and Time classes for creating a DateTime class. Understand the concept of inheriting properties from multiple parent classes in object-oriented programming."

  • C++
  • Object-Oriented Programming
  • Inheritance
  • Date Time

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Multiple Inheritance Andy Wang Object Oriented Programming in C++ COP 3330

  2. Multiple Inheritance C++ supports multiple inheritance A class can inherit properties from more than one base class (multiple parent classes) Not all object-oriented languages support multiple inheritance (e.g., Java)

  3. data.h #ifndef _DATE_H #define _DATE_H class Date { protected: Inline functions: code inserted at each invocation point int day, month, year; public: Date() { day = month = 1; year = 1900; } Date(int d, int m, int y) { day = d; month = m; year = y; } int getDay() const { return day; } int getMonth() const { return month; } int getYear() const { return year; } }; #endif

  4. time.h #ifndef _TIME_H #define _TIME_H class Time { protected: public: }; #endif int hour, min, sec; Time() { hour = min = sec = 0; } Time(int h, int m, int s) { hour = h; min = m; sec = s; } int getHour() const { return hour; } int getMin() const { return min; } int getSec() const { return sec; }

  5. datetime.h #ifndef _DATETIME_H #define _DATETIME_H const int DT_SIZE = 20; class DateTime : public Date, public Time { protected: char dateTimeString[DT_SIZE]; public: DateTime(); DateTime(int, int, int, int, int, int); const char *getDateTime() const { return dateTimeString; } }; #endif

  6. datetime.cpp #include <cstring> #include <cstdlib> #include DateTime.h const int TEMP_SIZE = 10; DateTime::DateTime() : Date(), Time() { strcpy(dateTimeString, 1/1/1900 0:0:0 ); }

  7. datetime.cpp DateTime::DateTime(int dy, int mon, int yr, int hr, int mt, int sc) : Date(dy, mon, yr), Time(hr, mt, sc) { TEMP_SIZE); strcat(dateTimeString, / ); strcpy(dateTimeString, itoa(getDay(), temp, TEMP_SIZE); strcat(dateTimeString, / ); strcpy(dateTimeString, itoa(getYear(), temp, TEMP_SIZE); strcat(dateTimeString, ); char temp[TEMP_SIZE]; strcpy(dateTimeString, itoa(getMonth(), temp,

  8. datetime.cpp strcpy(dateTimeString, itoa(getHour(), temp, TEMP_SIZE); strcat(dateTimeString, : ); strcpy(dateTimeString, itoa(getMin(), temp, TEMP_SIZE); strcat(dateTimeString, : ); strcpy(dateTimeString, itoa(getSec(), temp, TEMP_SIZE); }

  9. main.cpp #include <iostream> #include DateTime.h using namespace std; int main() { DateTime emptyDay; cout << emptyDay.getDateTime() << endl; } DateTime pastDay(2, 4, 60, 5, 32, 27); cout << pastDay.getDateTime() << endl; return 0;

More Related Content