Skip to content

Instantly share code, notes, and snippets.

@javedbaloch4
Created June 19, 2020 22:52
Show Gist options
  • Save javedbaloch4/495eac873eea94db040b25fa026d0b58 to your computer and use it in GitHub Desktop.
Save javedbaloch4/495eac873eea94db040b25fa026d0b58 to your computer and use it in GitHub Desktop.
C++ Run Time Polymorphism example with Customer & Customer Data class.
// Writting by Javed Ahmed (F2019266402) - June 20 2020 3: 50 AM
#include<iostream>
using namespace std;
class PersonData {
private:
string firstName;
string lastname;
string address;
string city;
string state;
int zip;
int phone;
public:
PersonData () { // Default Cons
firstName = "";
lastname = "";
address = "";
city = "";
state = "";
zip = 0;
phone = 0;
}
PersonData (string firstName, string lastname, string address, string city, string state, int zip, int phone) { // Overloaded Cons
this->firstName = firstName;
this->lastname = lastname;
this->address = address;
this->city = city;
this->state = state;
this->zip = zip;
this->phone = phone;
}
// Setters
void setFirstName (string firstName) {
this->firstName = firstName;
}
void setLastname (string lastname) {
this->lastname = lastname;
}
void setaddress (string address) {
this->address = address;
}
void setCity (string city) {
this->city = city;
}
void setState (string state) {
this->state = state;
}
void setZip (int zip) {
this->zip = zip;
}
void setPhone (int phone) {
this->phone = phone;
}
// Getters
string getFirstName() {
return this->firstName;
}
string getLastName() {
return this->lastname;
}
string getAddress() {
return this->address;
}
string getCity() {
return this->city;
}
string getState() {
return this->state;
}
int getZip() {
return this->zip;
}
int getPhone() {
return this->phone;
}
virtual void displayInfo () {
this->getFullName();
cout << "Address : " << this->address << endl;
cout << "City : " << this->city << endl;
cout << "State : " << this->state << endl;
cout << "Zip : " << this->zip << endl;
cout << "Phone : " << this->phone << endl;
}
// Oter Functions
protected:
void getFullName() {
cout << this->firstName << " " << this->lastname << endl;
}
void hr() {
cout << "\n \n ------------------------------- \n \n";
}
};
class CustomerData : public PersonData {
private:
int customerNumber;
bool mailingList;
public:
CustomerData () { // Customer Default Constructor
this->customerNumber = 0;
this->mailingList = false;
}
CustomerData (int customerNumber, bool mailingList, string firstName, string lastName, string address, string city, string state, int zip, int phone)
: PersonData ( firstName, lastName, address, city, state, zip, phone ) {
this->customerNumber = customerNumber;
this->mailingList = mailingList;
}
// Setters
void setCustomerNumber(int customerNumber) {
this->customerNumber = customerNumber;
}
void setMailingList(bool mailingList) {
this->mailingList = mailingList;
}
// Getters
int getCustomerNumber () {
return this->customerNumber;
}
bool getMailingList() {
return this->mailingList;
}
// Overridding Display Info
virtual void displayInfo () {
cout << "Customer Serial Number : " << this->customerNumber << endl;
cout << "Full Name : ";
this->getFullName();
cout << "Address : " << this->getAddress() << endl;
cout << "City : " << this->getCity() << endl;
cout << "State : " << this->getState() << endl;
cout << "Zip : " << this->getZip() << endl;
cout << "Phone : " << this->getPhone() << endl;
if (this->mailingList == true) {
cout << " *** You're on Mailing List *** ";
} else {
cout << " *** You're not on Mailing List *** ";
}
this->hr();
}
};
int main() {
// Making yourself on maling list
bool mailingList = false;
// Main class & run time polymorphism
PersonData *prData;
// Lets create customer object
prData = new CustomerData(402, mailingList, "Javed","Ahmed","Sariab Road","Quetta","Balochistan",50003,034017670);
prData->displayInfo();
// Lets create another customer object
prData = new CustomerData(401, true, "Ijaz","Ahmed","Shahn Kabul","Kabul","Kabul Afghanistan",99323,0234234234);
prData->displayInfo();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment