Skip to content

Instantly share code, notes, and snippets.

@GermanHoyos
Created September 19, 2022 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GermanHoyos/293a7795001bef0bdd512c0eb82099d2 to your computer and use it in GitHub Desktop.
Save GermanHoyos/293a7795001bef0bdd512c0eb82099d2 to your computer and use it in GitHub Desktop.
Working with pointers in C++
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main() {
//*************************************************************
class Student { //*********************************************
public:
string student_ID, first_name;
// setters
void set_student_ID(string student_ID){ this->student_ID = student_ID; }
void set_first_name(string first_name){ this->first_name = first_name;}
// getters
string get_student_ID() { this->student_ID = student_ID; };
string get_first_name() { this->first_name = first_name; };
void print(){ cout << this->get_student_ID(); };
// constructor
Student(string student_ID, string first_name){
this-> student_ID = student_ID;
this-> first_name = first_name;
};
}; //**********************************************************
//*************************************************************
//*************************************************************
class Roster { //**********************************************
public:
int delimCount = 0;
string str_student_data;
Student* classRosterArray[5];
// "studentData[]" parser / then call add()
void parse(int i, string arrRow){
string sendID, sendFName;
for (char c : arrRow){
//cout << c;// print all chars
if (c == ','){ delimCount++; }
if (delimCount == 0){
sendID = arrRow.substr(0,2);
}
if (delimCount == 1){
sendFName = arrRow.substr(4,(arrRow.length() - 4));
}
} cout << endl; // break by index
delimCount = 0;
add( i ,sendID, sendFName);
};
// "classRosterArray[]" builder / new "Student" object builder
void add(int i, string student_ID, string first_name) {
//cout<< i << " " << student_ID << " " << first_name;
classRosterArray[i] = new Student (student_ID, first_name);
cout << classRosterArray[i];
};
// remove student from "classRosterArray[]" or "Student"
void remove() {
};
// print current state of roster
void printAll(){
for (int i = 0; i < 5; i++){
//how can I dereference "classRosterArray[i]" to see instance of student object?
cout << "address is -> " << classRosterArray[i] << " student is -> " << endl;
}
}
// constructor
Roster(){};
}; //*********************************************************
//*************************************************************
//array
const string studentData[] = {
"A1, John",
"A2, Suzan",
"A3, Jack",
"A4, Erin",
"A5, German"
};
//console out array
for (int i = 0; i < 5; i++){
cout << &studentData[i] << ": " << studentData[i] << endl;
}
//instantiate roster
Roster roster;
//parse array
for (int i = 0; i < 5; i++){
roster.parse(i, studentData[i]);
} cout << endl << endl;
//print array
roster.printAll();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment