2018年8月5日 星期日

Operator Overloading the Stream Insertion Operator

Money.h

#ifndef _MONEY_MONEY_H
#define _MONEY_MONEY_H


#include <iostream>

class Money {
   
    int dollars;
    int cents;
public:
    Money(int dollars, int cents);
    Money(int total);


    //----DO NOT MODIFY THE CODE ABOVE THIS LINE----
    //----WRITE YOUR METHOD DECLARATIONS BELOW THIS LINE----
   
    friend std::ostream &operator<<(std::ostream &os, const Money &rhs);
    //----WRITE YOUR METHOD DECLARATIONS ABOVE THIS LINE----
    //----DO NOT MODIFY THE CODE BELOW THIS LINE----
};


#endif //_MONEY_MONEY_H


=================================================

Money.cpp

#include "Money.h"

Money::Money(int dollars, int cents) : dollars{dollars}, cents{cents} {}

Money::Money(int total) : dollars {total/100}, cents{total%100}  {}
 

//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
//----WRITE YOUR METHOD DEFINITIONS BELOW THIS LINE----

std::ostream &operator<<(std::ostream &os, const Money &rhs) {
    os << "dollars: " << rhs.dollars << " cents: " << rhs.cents;
    return os;
}

//----WRITE YOUR METHOD DEFINITIONS ABOVE THIS LINE----


Operator Overloading as Non-member Functions

Money.cpp

#include "Money.h"

Money::Money(int dollars, int cents) : dollars{dollars}, cents{cents} {}

Money::Money(int total) : dollars {total/100}, cents{total%100}  {}
 

//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
//----WRITE YOUR METHOD DEFINITIONS BELOW THIS LINE----

Money Money::operator+(const Money &p)const{
    int dollars_temp = this->dollars + p.dollars;
    int cents_temp = this->cents + p.cents;
    if(cents_temp > 100.0){
        dollars_temp += cents_temp / 100;
        cents_temp %= 100;
    }
    Money temp(dollars_temp, cents_temp);
    return temp;
}

//----WRITE YOUR METHOD DEFINITIONS ABOVE THIS LINE----



=========================================

Money.h

#ifndef _MONEY_MONEY_H
#define _MONEY_MONEY_H


#include <iostream>

class Money {
    int dollars;
    int cents;
public:
    Money(int dollars, int cents);
    Money(int total);
    int get_dollars() const {return dollars;}
    int get_cents() const {return cents; }
   

    //----DO NOT MODIFY THE CODE ABOVE THIS LINE----
    //----WRITE YOUR METHOD DECLARATIONS BELOW THIS LINE----
    Money operator+(const Money &p)const;


   
    //----WRITE YOUR METHOD DECLARATIONS ABOVE THIS LINE----
    //----DO NOT MODIFY THE CODE BELOW THIS LINE----
};


#endif //_MONEY_MONEY_H

Operator Overloading as Member Functions

Money.cpp

#include "Money.h"

Money::Money(int dollars, int cents) : dollars{dollars}, cents{cents} {}

Money::Money(int total) : dollars {total/100}, cents{total%100}  {}
 

//----DO NOT MODIFY THE CODE ABOVE THIS LINE----
//----WRITE YOUR METHOD DEFINITIONS BELOW THIS LINE----

bool Money::operator==(const Money &p)const{
    if((this->dollars == p.dollars) && (this->cents == p.cents))return true;
 
    else return false;
}
 
bool Money::operator!=(const Money &p)const{
   if((this->dollars != p.dollars) && (this->cents != p.cents))return true;
 
    else return false;
}

//----WRITE YOUR METHOD DEFINITIONS ABOVE THIS LINE----



==============================================
Money.h

#ifndef _MONEY_MONEY_H
#define _MONEY_MONEY_H


#include <iostream>

class Money {
    int dollars;
    int cents;
public:
    Money(int dollars, int cents);
    Money(int total);

    //----DO NOT MODIFY THE CODE ABOVE THIS LINE----
    //----WRITE YOUR METHOD DECLARATIONS BELOW THIS LINE----
    bool operator==(const Money &a)const;
 
    bool operator!=(const Money &a)const;
 
    //----WRITE YOUR METHOD DECLARATIONS ABOVE THIS LINE----
    //----DO NOT MODIFY THE CODE BELOW THIS LINE----
};


#endif //_MONEY_MONEY_H

2018年7月30日 星期一

Add a Copy Constructor to an Existing Class

/* Dog.h */


#ifndef __DOG_H__
#define __DOG_H__
#include <string>
using namespace std;

class Dog {
private:
    string name;
    int age;
public:
        Dog(string name, int age) : name{name}, age{age} { }

//---- WRITE YOUR COPY CONSTRUCTOR BELOW THIS LINE
        Dog(Dog&);


//---- WRITE YOUR COPY CONSTRUCTOR ABOVE THIS LINE
    string get_name() {return name; }
    void set_name(string n) {name = n; }
    int get_age() {return age; }
    void set_age(int a) { age = a;}
    int get_dog_years() { return age * 7; }
    string speak() { return "Woof";}
};
#endif




========================================

/* exercise.cpp */



// ----THIS FILE IS EMPTY----
#include "Dog.h"


Dog::Dog(Dog &dog):name{dog.name}, age{dog.age}{
    printf("Copy Constructor");
}

Add an Overloaded Constructor to an Existing Class

/* Dog.h */

// THIS FILE IS EMPTY ----
#include "Dog.h"

Dog::Dog(std::string name1, int age1){
   
    name = name1;
    age = age1;
}



========================================

/* exercise.cpp */


#ifndef __DOG_H__
#define __DOG_H__
#include <string>
using namespace std;

class Dog {
private:
    string name;
    int age;
public:

//---- WRITE YOUR OVERLOADED CONSTRUCTOR BELOW THIS LINE

Dog();
Dog(string name1, int age1);

//---- WRITE YOUR OVERLOADED CONSTRUCTOR ABOVE THIS LINE
    string get_name() {return name; }
    void set_name(string n) {name = n; }
    int get_age() {return age; }
    void set_age(int a) { age = a;}
    int get_dog_years() { return age * 7; }
    string speak() { return "Woof";}
};
#endif

Add a Default Constructor to an Existing Class

/* Dog.h */


#ifndef __DOG_H__
#define __DOG_H__
#include <string>
using namespace std;

class Dog {
private:
    string name;
    int age;
public:
//---- WRITE YOUR NO-ARGS CONSTRUCTOR BELOW THIS LINE----
Dog();


//---- WRITE YOUR NO_ARGS CONSTRUCTOR ABOVE THIS LINE----
//---- DO NOT MODIFY THE CODE BELOW THIS LINE


    string get_name() {return name; }
    void set_name(string n) {name = n; }
    int get_age() {return age; }
    void set_age(int a) { age = a;}
    int get_dog_years() { return age * 7; }
    string speak() { return "Woof";}
};


#endif



========================================

/* exercise.cpp */


// THIS FILE IS EMPTY----
#include "Dog.h"
Dog::Dog(){
    name = "None";
    age = 0;
}

2018年7月29日 星期日

Add more public methods to an existing class


/* Dog.h */

#ifndef __DOG_H__
#define __DOG_H__
#include <string>
using namespace std;

class Dog {
private:
    string name;
    int age;
public:

    string get_name() {return name; }
    void set_name(string n) {name = n; }
    int get_age() {return age; }
    void set_age(int a) { age = a;}
    //---- WRITE YOUR CLASS FUNCTIONS BELOW THIS LINE----
    int get_human_years();
    string speak();
   
    //---- WRITE YOUR CLASS FUNCTIONS ABOVE THIS LINE----
};
#endif

========================================

/* exercise.cpp */

// THIS FILE IS EMPTY----
#include "Dog.h"

int Dog::get_human_years(){return  7*age;}
std::string Dog::speak(){
        return "Woof";
    }