Haste makes waste

Nano01(自動運転)-【C++】-Lesson08-Classes类

Posted on By lijun

5. Classes

The default is to make all members private. This means only other members of the class can access the data.

class Student
{
        string name;
        int id;
        int gradDate;

    public:
        void setName(string nameIn);
        void setId(int idIn);
        void setGradDate(int dateIn);
        string getName();
        int getId();
        int getGradDate();
        void print();
};

void Student::setName(string nameIn)
{
     name = nameIn;
}

void Student::setId(int idIn)
{
     id = idIn;
}

void Student::setGradDate(int gradDateIn)
{
     gradDate = gradDateIn;
}

string Student::getName()
{
     return name;
}

int Student::getId()
{
     return id; 
}

int Student::getGradDate()
{
     return gradDate;
}

10. Using a Class

/*Goal: Practice using a class*/

#include<iostream>
using namespace std;


class Student
{
        string name;
        int id;
        int gradDate;

    public:
        void setName(string nameIn);
        void setId(int idIn);
        void setGradDate(int dateIn);
        string getName();
        int getId();
        int getGradDate();
        void print();
};

void Student::setName(string nameIn)
{
     name = nameIn;
}

void Student::setId(int idIn)
{
     id = idIn;
}

void Student::setGradDate(int gradDateIn)
{
     gradDate = gradDateIn;
}

void Student::print()
{
     cout<<name<<" "<<id<<" "<<gradDate;
}

string Student::getName()
{
     return name;
}

int Student::getId()
{
     return id; 
}

int Student::getGradDate()
{
     return gradDate;
}

int main()
{
    int integer1; 
    float float1;
    Student student1; 
    
    integer1 = 4; //assign a value to integer1
    float1 = 4.333; //assign a value to float1

    student1.setName("Catherine Gamboa"); //assign a value to the student name
    student1.setId(54345); //assign a value to the student id number
    student1.setGradDate(2017); //assign a value to the student grad date
    
    //Let's print the values of our variables
    cout<<"integer1 = "<<integer1<<"\n";
    cout<<"float1 = "<<float1<<"\n\n";
    
    //There are two ways we can print the values of our class:
    //The first is to call the print function we created.
    cout<<"Using the Student::print function\n";
    cout<<"Student1 = ";
    student1.print();
    cout<<"\n\n";
    
    //The second is to access each member of the class using the get functions
    cout<<"Using the student access functions\n";
    cout<<"Student1 name = "<<student1.getName()<<"\n";
    cout<<"Student1 ID = "<<student1.getId()<<"\n";
    cout<<"Student1 Grad Date = "<<student1.getGradDate()<<"\n";
    
    
    return 0; 
}

16. Constructors

**There is a special function member that we need to talk about, constructors. **

A constructor is special function that is executed whenever we create a new instance of the class. It is used to set initial values of data members of the class.

17. Destructors

Destructors are special class functions that are called whenever an object goes out of scope. Just like a constructor, a destructor is called automatically.

Destructors cannot:

  • return a value
  • accept parameters

main.hpp:

#include<iostream>
using namespace std;

class Dog
{
    private:
        int license;
    public:
        Dog();
        Dog(int licenseIn);
        void setLicense(int licenseIn);
        int getLicense();
        ~Dog();
};

Dog::Dog()
{
   cout<<"\nCreating the dog";
   license = 0;
}

Dog::~Dog()
{
    cout<<"\nDeleting the dog1";
}
Dog::Dog(int licenseIn)
{
   license = licenseIn; 
}
void Dog::setLicense(int licenseIn)
{
    license = licenseIn;
}
int Dog::getLicense()
{
    return license;
}

main.cpp:

#include "main.hpp"

int main()
{
    Dog d1 = Dog();
    Dog d2(666666);
    cout<<d2.getLicense();
    return 0;
}
Creating the dog666666
Deleting the dog
Deleting the dog

上面的类中,重载了两个构造函数,根据传递的参数类型选择不同的构造函数。

19.

The class has four public members that are functions and a constructor:

  • the constructor sets each value of gameSpace to the char value ‘-‘
  • each position to be set with a single char
  • each position can be read individually
  • printInfo will print gameSpace as a 4x4 matrix
  • the helper function ‘fourInRow’ checks every row, looking for four ‘x’s. When it finds four x’s in the same row, it returns a ‘1’, otherwise it returns a ‘0’.

main.hpp:

/*header file for main.cpp*/
#include <iostream>
#include <iomanip> 
using namespace std;

class Gameboard
{
    char gameSpace[4][4];
public:
    Gameboard();
    void setGameSpace(int row,int column, char value);
    char getGameSpace(int row,int column);
    int fourInRow();
    void printInfo();
};

Gameboard::Gameboard()
{
    for(int i=0;i<4; i++)
        for(int j=0;j<4; j++)
        {
            gameSpace[i][j] = '-';
        }
}
void Gameboard::setGameSpace(int row,int column,char value)
{
    gameSpace[row][column] = value;
}

char Gameboard::getGameSpace(int row,int column)
{
    return gameSpace[row][column];
}

int Gameboard::fourInRow()
{
    int count;
    for(int i=0;i<4; i++)
    {
        count = 0;
        for(int j=0;j<4; j++)
        {
            if(gameSpace[i][j]=='x')
            {
                count++;
                //cout<<"count = "<<count;
            }
        }
        if(count == 4)
            return 1;
    }
    return 0;
}
void Gameboard::printInfo()
{
    cout<<std::setw(5);
    cout<<"\n";
    for(int i=0;i<4; i++)
    {
        for(int j=0;j<4; j++)
        {
            cout<<gameSpace[i][j];
        }
        cout<<"\n";
    }
}

main.cpp:

#include "main.hpp"

int main()
{
    Gameboard game1;
    game1.setGameSpace(0,0,'x');
    game1.setGameSpace(0,1,'x');
    game1.setGameSpace(0,2,'x');
    game1.setGameSpace(0,3,'y');
    game1.setGameSpace(1,0,'x');
    game1.setGameSpace(2,0,'x');
    game1.setGameSpace(3,0,'x');
    game1.setGameSpace(3,1,'x');
    game1.setGameSpace(3,2,'x');
    game1.setGameSpace(3,3,'x');
    
    if(game1.fourInRow() == 1)
    {
        cout<<"X got four in a row! \n\n";
    }
    else
    {
        cout<<"X did not get four in a row :(\n\n";
    }
    game1.printInfo();
    return 0;
}

26. this Pointer

#include <iostream>
 
using namespace std;

class Shape {
   public:
      // Constructor definition
      Shape(int l = 2, int w = 2) 
      {
         length = l;
         width = w;
      }
		
      double Area() 
      {
         return length * width;
      }
		
	  //Use 'this' to compare areas
      int compareWithThis(Shape shape) 
      {
         return this->Area() > shape.Area();
      }

      //'this' is not necessary to compare shapes
      int compare(Shape shapeIn) 
      {
         return Area() > shapeIn.Area();
      }
      
   private:
      int length;     // Length of a box
      int width;
};

int main(void) 
{
   Shape sh1(4, 4);    // Declare shape1
   Shape sh2(2, 6);    // Declare shape2

   if(sh1.compare(sh2)) 
   {
      cout << "\nShape2 is smaller than Shape1" <<endl;
   } 
   else 
   {
      cout << "\nShape2 is equal to or larger than Shape1" <<endl;
   }

   if(sh1.compareWithThis(sh2)) {
      cout << "\nShape2 is smaller than Shape1" <<endl;
   } 
   else 
   {
      cout << "Shape2 is equal to or larger than Shape1" <<endl;
   }
   
   return 0;
}