/* 
* Code Snippets for this week
* Small programs to show bits of C and bits of C. 
* >> I've tried to put * at the start of all the comment lines.
* >> I wrote this without a compiler, so might need tweaks.  
*/

/*
* Example Interface / Header File (student.h) 
*/
#ifndef _EXAMPLE_H_
#define _EXAMPLE_H_



class Student
{
private:
    static const int NAME_MAX = 32;
    char name[NAME_MAX];
    int mark;
public:
    Student();
    Student(char* name, int mark); 
   ~Student();
   
    void setMark(int m);
    int getMark();
    void setName(char* n);
    char* getName(); /* Not good */
    int getName(char* outBuffer, int bufferSize); /* better */
    void printStudent(Student s);
    void fillStudent(Student *s);
}; 

/* 
* Example Implementation File (student.cpp)
*/

#include <stdio.h>
#include "student.h"
#include "string.h"

Student::Student()
{
    strcpy(name,"Not set");
    mark = -1;
}

Student::Student(char* name, int mark)
{
    setName(name);
    setMark(mark);
}

void Student::setMark(int mark)
{
    /* C++ will assume you mean the local variable if it has
    * the same name as a class attribute unless you use this->
    */
    this->mark = mark;
}

int Student::getMark()
{
    return this->mark;
}

void Student::setName(char* str)
{
    strcpy(this->name,str);
}

/* This is not a good way to do this once you have a 
* pointer to the private you can change it. Bad. 
*/
char* Student::getName()
{
    return this->name;
}

/* Better way */
int Student::getName(char* nameOut, int bufferSize)
{
    if((strlen(this->name)+1) < bufferSize)
    {
        strcpy(nameOut, this->name);
        return strlen(this->name);
    }
    else
    {
        return -1;
    }
}

void Student::printStudent()
{
    /* this is a kind of magic pointer to the current object */
    printf("%s is paid %d\n", this->getName(), this->getMark());
}

void fillStudent()
{
    char inName[NAME_MAX];
    int inMark
    scanf("%s", inName);
    scanf("%d", inMark);

    this->setName(inName);
    this->setMark(inMark);
}

/* 
* Example Client File (main.cpp)
*/

#include <stdio.h>
#include "student.h"

int main(char* argv, int argc)
{
    Student *s = new Student();
    s->fillStudent();  /* uses the sets */
    s->printStudent();  /* uses the gets */
    delete s;

    exit(0);
}

