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";
    }

Adding public methods that access private class attributes


/* Dog.h */

#ifndef _DOG_H_

#define _DOG_H_
#include <string>


class Dog{
private: 

    std::string name;
    int age;
public:
    void set_name(std::string name1);

    std::string get_name();



    void set_age(int ag);

    int get_age();
};


#endif



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

/* exercise.cpp */

#include "Dog.h"


void Dog::set_name (std::string name1){
    name = name1;
}

std::string Dog::get_name() {return name;}



int Dog::get_age() {return age;}

void Dog::set_age (int ag){
    age = ag;
}

2018年7月28日 星期六

Creating and Accessing Object

#include "Dog.h"


Dog test_dog() {
//---- WRITE YOUR CODE BELOW THIS LINE----
Dog spot;
spot.name = "Spot";
spot.age = 5;



//---- WRITE YOUR CODE ABOVE THIS LINE----
//---- DO NOT CHANGE THE CODE BELOW----
    return spot;
}

2018年7月21日 星期六

Using C++ Strings Exercise 2

#include <iostream>
#include <string>
using namespace std;

void cpp_strings() {
   
    string journal_entry_1 {"Isaac Newton"};
    string journal_entry_2 {"Leibniz"};
   
    //----DO NOT MODIFY THE CODE ABOVE THIS LINE----
    //----WRITE YOUR CODE BELOW THIS LINE----
    journal_entry_1.erase(0,6);
   
    if(journal_entry_1 > journal_entry_2)
    journal_entry_1.swap(journal_entry_2);
    //----WRITE YOUR CODE ABOVE THIS LINE----
    //----DO NOT MODIFY THE CODE BELOW THIS LINE----
   
    cout << journal_entry_1 << "\n" << journal_entry_2;
}

Using C++ Strings Exercise 1

#include <iostream>
#include <string>
using namespace std;

void cpp_strings() {
   
    string unformatted_full_name {"StephenHawking"};
   
    //----DO NOT MODIFY THE CODE ABOVE THIS LINE----
    //----WRITE YOUR CODE BELOW THIS LINE----
    string first_name{}, last_name{}, formatted_full_name{};
   
    first_name = unformatted_full_name.substr(0, 7);
    last_name = unformatted_full_name.substr(7, 14);
    formatted_full_name = first_name + " " + last_name;
    //----WRITE YOUR CODE ABOVE THIS LINE----
    //----DO NOT MODIFY THE CODE BELOW THIS LINE----
   
    cout << formatted_full_name;
}

Using C-style Strings

#include <iostream>
#include <cstring>
using namespace std;

void strings_and_functions() {
   
    //----WRITE YOUR CODE BELOW THIS LINE----
    char whole_name[50] {}, first_name[50] {"Bjarne"}, last_name[50] {"Stroustrup"};
    int first_name_length{}, last_name_length{}, whole_name_length{};
   
   
    first_name_length = strlen(first_name);
    last_name_length = strlen(last_name);
    strcpy(whole_name, first_name);
    strcat(whole_name, last_name);
    whole_name_length = strlen(whole_name);
   
    //----WRITE YOUR CODE ABOVE THIS LINE----
    //----DO NOT MODIFY THE CODE BELOW THIS LINE----
   
    cout << "The length of the first name, " << first_name << ", is " << first_name_length << " letters long and the length of the last name, " << last_name << ", is " << last_name_length << " letters long. This means that the length of the whole name must be " << whole_name_length << " letters long.";
}

2018年7月20日 星期五

Sum of the Product of all Pairs of Vector Elements

#include <vector>
using namespace std;

int calculate_pairs(vector<int> vec) {
    //----WRITE YOUR CODE BELOW THIS LINE----
    int result {0};
    for(int i=0; i<vec.size(); i++)
        for(int j = i + 1; j < vec.size(); j++)
            result += vec[i] * vec[j];
   
    //----WRITE YOUR CODE ABOVE THIS LINE----
    //----DO NOT MODIFY THE CODE BELOW THIS LINE----
    return result;
}

while loop exec -99

#include <iostream>
#include <vector>
using namespace std;

int count_numbers(const vector<int> &vec) {
    //---- WRITE YOUR CODE BELOW THIS LINE----
    int count{0};
    for(auto v:vec){
        if(v !=-99)
        count ++;
        if (v == -99)
        break;
    }
    //---- WRITE YOUR CODE ABOVE THIS LINE----
    //---- DO NOT MODIFY THE CODE BELOW THIS LINE-----
    return count;
}

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

range based for loop

#include <vector>
using namespace std;

int count_divisible() {
   
    vector<int> vec {1,3,5,15,16,17,18,19,20,21,25,26,27,30,50,55,56,58,100,200,300,400,500,600,700};
    //---- WRITE YOUR CODE BELOW THIS LINE----
    int count{0};
    for(auto num:vec){
        count += ((!(num%3)||!(num%5))? 1: 0);
    }
   
   
    //---- WRITE YOUR CODE ABOVE THIS LINE----
    //---- DO NOT CHANGE THE CODE BELOW THIS LINE----
    return count;
}

Day of the week - beginning-c-plus-plus-programming

#include <iostream>
using namespace std;

void display_day(int day_code) {
    //----WRITE YOUR CODE BELOW THIS LINE----
 
    switch(day_code){
        case 0:
        cout << "Sunday";
        break;
        case 1:
        cout << "Monday";
        break;
        case 2:
        cout << "Tuesday";
        break;
        case 3:
        cout << "Wednesday";
        break;
        case 4:
        cout << "Thursday";
        break;
        case 5:
        cout << "Friday";
        break;
        case 6:
        cout << "Saturday";
        break;
        default:
        cout << "Error - illegal day code";
       
    }
   
   
    //----WRITE YOUR CODE ABOVE THIS LINE----
}

2018年7月19日 星期四

Car driving - Beginning C++ Programming - From Beginner to Beyond

https://ideone.com/OqaY2V

#include <iostream>
using namespace std;

void logical_operators(int age, bool parental_consent, bool ssn, bool accidents) {
 
    //----WRITE YOUR CODE BELOW THIS LINE----
 
    if (((age > 15) && parental_consent && ssn && (!accidents))||
    ((age >= 18) && (1 || parental_consent) && ssn && (!accidents)))//WRITE ALL YOUR CODE WITHIN THE PARENTHESES
        cout << "Yes, you can work.";
 
    //----WRITE YOUR CODE ABOVE THIS LINE----
    return;
}
int main(){

return 0;
}
//this is a solution for "Beginning C++ Programming - From Beginner to Beyond" in ch3,"Statement and Operator"