CSE 201 - Tutorial 5

Exercise 1: Organizing files

Reorganize the following code, so that you have: Base.hpp, Base.cpp, Derived.hpp, Derived.cpp and main, then compile with a makefile (to do). Look carefully how inheritance works for constructors.


#include<iostream>
class Base{
    public:
        int x;
        int y;
        double z;
        Base(){};
        Base(int a, int b, double c ){
                this->x=a;
                this->y=b;
                this->z=c;
            }
        ~Base(){};
};      
    
class Derived : public Base {
    public:
        double w;
        // Look at this 
        Derived(int a, int b, double c, double d):Base(a,b,c){
            this->w = d;            
        };;
};

int main(){
    Derived d(1,2,3.01,4.02);
    std::cout<< d.x <<std::endl;
    std::cout<< d.y <<std::endl;
    std::cout<< d.z <<std::endl;
    std::cout<< d.w <<std::endl;
}

Exercise 2: Inheriting Costructors

Download shape.hpp,polygon.hpp, main.cpp. Then write a proper shape.cpp and polygon.cpp so that you can compile your program. Pay attention to namaspaces.

Exercise 3: Type of inheritance

The following code has a bug. If you try to compile it, you get errors. Fix it.

#include<iostream>

class A {

    int i;
    
public:
    
    int getI(){ return i; }
    void setI(int i){ this->i = i; }

};

class B : A {
    
    int j;

public:
    
    int getJ(){ return j; }
    void setJ(int j){ this->j=j; }
    
};  

int main(){

    B b;
    b.setI(10);
        
    std::cout << b.getI() << std::endl;
    return 0;

}

Exercise 4: Function templates

Complete the following code so that it compiles and the output is:

>Equal(i1, i2): 1
>Equal(d1, d2): 0
>Equal(s1, s2): 1
>Equal(a1, a2): 1
>Equal(a1, a3): 0
#include <iostream>
#include <string>
using namespace std;

class A {

    public:
        int i;
        A(){};
        A(int a){
            this->i=a;
        }   
        ~A(){}; 
        
        /* To do: begin */
            something missing...
        /* To do: end  */

    private:    
        int getI(){ return i;}
        void setI(int a){
            this->i=a;
        }   

};

/* To do: begin */
    something missing...
/* To do: end */

int main (int argc, char* argv[]) {
    int i1 = 10;
    int i2 = 10;
    cout << "Equal(i1, i2): " << Equal(i1, i2) << endl; 

    double d1 = 10.5; 
    double d2 = 3.7; 
    cout << "Equal(d1, d2): " << Equal(d1, d2) << endl; 
   
    string s1 = "Hello"; 
    string s2 = "Hello"; 
    cout << "Equal(s1, s2): " << Equal(s1, s2) << endl; 
   
    A a1(1), a2(1), a3(2);
    cout << "Equal(a1, a2): " << Equal(a1, a2) << endl; 
    cout << "Equal(a1, a3): " << Equal(a1, a3) << endl; 

    return 0;
    
}

Exercise 5: (Reminder) Matrices and overloading operators

You are given the following matrix.hpp and mainM.cpp. Write the proper matrix.cpp.

#include <iostream>

class Matrix{
    private:
    int rows;
    int cols;
    int **Mat;

    public:
        Matrix (const int &rows,const int &cols);
        ~Matrix ();
        
        void setMatrixValue(int r, int c, int value);
        int  getMatrixValue(int r, int c) const;
        int getMatrixRows() const;
        int getMatrixCols() const;
        
        Matrix  operator +(const Matrix &other) const ;
        Matrix  operator *(const Matrix &other) const;
        
};

std::ostream& operator<<(std::ostream &os, const Matrix &m);

#include "Matrix.hpp"
#include <iostream>

int main(){
    
    Matrix m1(2, 2);
    m1.setMatrixValue(0,0,1);
    m1.setMatrixValue(0,1,0);
    m1.setMatrixValue(1,0,1);
    m1.setMatrixValue(1,1,-1);
    
    Matrix m2(2, 2);
    m2.setMatrixValue(0,0,1);
    m2.setMatrixValue(0,1,0);
    m2.setMatrixValue(1,0,0);
    m2.setMatrixValue(1,1,1);
    
    std::cout << m1 << std::endl;
    std::cout << m2 << std::endl;
    
    std::cout << m1+m2 << std::endl;
    std::cout << m2+m1 << std::endl;
    std::cout << m1*m2 << std::endl;
    std::cout << m2*m1 << std::endl;
    
    return 0 ; 
}