Schedule‎ > ‎

12A Class Student

Here is completed code:  ClassStudent.ino,  ClassStudent.h and ClassStudent.cpp

ClassStudent.h
#ifndef CLASSSTUDENT_H
#define CLASSSTUDENT_H

#include "Arduino.h"

class Student  {
public:
  Student(String name, int ID, String hobby);
  Student(String name, int ID);
  Student();
  void setName(String name);
  String getName() const;
  void setHobby(String hobby);
  String getHobby() const;
  void setID(int ID);
  int getID() const;
  void printStudent() const;

private:
  String _name;
  int _ID;
  String _hobby;
};
#endif

ClassStudent.cpp
#include "Arduino.h"
#include "ClassStudent.h"
#include <ArduinoSTL.h>
using namespace std;

Student::Student(String name, int ID, String hobby)  {
  _name = name;
  _ID = ID;
  _hobby = hobby;
}


Student::Student(String name, int ID)  {
  _name = name;
  _ID = ID;
  _hobby = "";
}

Student::Student()  {
  _name = "";
  _ID = 0;
  _hobby = "";
}
String Student::getHobby() const  {
  return _hobby;
}

void Student::setHobby(String hobby)  {
  _hobby = hobby;
}

String Student::getName() const  {
  return _name;
}

void Student::setName(String name)  {
  _name = name;
}

int Student::getID() const  {
  return _ID;
}

void Student::setID(int ID)  {
  _ID = ID;
}

void Student::printStudent() const {
  Serial.print(getName());
  cout << " ID " << getID() << " Hobby: ";
  Serial.println(getHobby());
}


ClassStudent.ino
#include <ArduinoSTL.h>
#include "ClassStudent.h"
using namespace std;

void setup() {
  Serial.begin(9600);
  Student student1("JoseG", 2345, "Studying");  //max value for ID is 32768
  Student student2("HansL", 7891, "Programming");
  Student student3("Sharon", 2121, "Teaching C++");
  student1.printStudent();
  student2.printStudent();
  student3.printStudent();
  student1.setName("Juan");
  student2.setHobby("Java");
  student1.printStudent();
  student2.printStudent();
  student3.printStudent();
}

void loop() {
  // put your main code here, to run repeatedly:

}




Comments