C++ Tutorial

Posted: April 6, 2011 in C++

//#ifdef GUARDIAN_H
//#define GUARDIAN_H

#include <iostream>
#include <string>
using namespace std;

class Guardian
{
private:
 string GFirstName,GLastName,GId,GPhone,GAddress;

public:
 
 Guardian();//default constructor
 Guardian(string FirstName,string LastName,string Id,string Phone,string Address);
 void setFirstName(string FirstName);
 string getFirstName();

 void setLastName(string LastName);
 string getLastName();

 void setId(string Id);
 string getId();

 void setPhone(string Phone);
 string getPhone();

 void setAddress(string Address);
 string getAddress();

};

Guardian::Guardian(){}

Guardian::Guardian(string FirstName, string LastName, string Id, string Phone, string Address)
{GFirstName=FirstName;GLastName=LastName;GId=Id;GPhone=Phone;GAddress=Address;}

void Guardian::setFirstName(string FirstName){GFirstName=FirstName;}
string Guardian::getFirstName(){return GFirstName;}

void Guardian::setLastName(string LastName){GLastName=LastName;}
string Guardian::getLastName(){return GLastName;}

void Guardian::setId(string Id){GId=Id;}
string Guardian::getId(){return GId;}

void Guardian::setPhone(string Phone){GPhone=Phone;}
string Guardian::getPhone(){return GPhone;}

void Guardian::setAddress(string Address){GAddress=Address;}
string Guardian::getAddress(){return GAddress;}
//#endif

==========================================

//#ifndef STUDENT_H //for avoid redefination for the header
//#define STUDENT_H

/*#include <iostream>
#include <string>
using namespace std;*/

#include “Guardian.h”

class Student
{
private:
 string SFirstName,SLastName,SId,SPhone,SAddress;
 
 Guardian G;//set a relationship from Guardian

public:
 Student();//default constructor
 Student(string Id, string Phone,string Address);//set the variable to the constructor

 void setFirstName(string FirstName);
 string getFirstName();

 void setLastName(string LastName);
 string getLastName();

 void setId(string Id);
 string getId();

 void setPhone(string Phone);
 string getPhone();

 void setAddress(string Address);
 string getAddress();

 /*——————Guardian—————————*/
 void setGuardian(Guardian Gd);
 Guardian getGuardian();
 /*——————Guardian—————————*/

};

Student::Student(){}

Student::Student(string Id,string Phone,string Address)//set initialization for constructor
{SId=Id;SPhone=Phone;SAddress=Address;}

void Student::setFirstName(string FirstName){SFirstName=FirstName;}
string Student::getFirstName(){return SFirstName;}

void Student::setLastName(string LastName){SLastName=LastName;}
string Student::getLastName(){return SLastName;}

void Student::setId(string Id){SId=Id;}
string Student::getId(){return SId;}

void Student::setPhone(string Phone){SPhone=Phone;}
string Student::getPhone(){return SPhone;}

void Student::setAddress(string Address){SAddress=Address;}
string Student::getAddress(){return SAddress;}
/*——————Guardian—————————*/
void Student::setGuardian(Guardian Gd){G=Gd;}
Guardian Student::getGuardian(){return G;}
/*——————Guardian—————————*/

//#endif

=========================================

#include “Student.h”

void main()
{
 Student Student1(“TP000001″,”0108888888″,”Bukit Jalil”);//set initialization

 string tmp;

 cout<<“Enter the firstname: “;
 cin>>tmp;
 Student1.setFirstName(tmp);

 cout<<“Enter the lastname: “;
 cin>>tmp;
 Student1.setLastName(tmp);

 cout<<endl<<“Your personal details:”<<endl;
 
 cout<<“Name: “<<Student1.getLastName()<<” ”
  <<Student1.getFirstName()<<endl;
 
 cout<<“ID: “<<Student1.getId()<<endl
  <<“Phone No: “<<Student1.getPhone()<<endl
  <<“Address: “<<Student1.getAddress()<<endl;

 Guardian Guardian1(“Huang”,”FeiHong”,”TP999999″,”0100000001″,”Bukit Jaili”);
 Student1.setGuardian(Guardian1);
 
 cout<<endl<<“Guardian personal details:”<<endl;
 
 cout<<“Name: “<<Student1.getGuardian().getFirstName()<<” ”
  <<Student1.getGuardian().getLastName()<<endl;
 
 cout<<“ID: “<<Student1.getGuardian().getId()<<endl
  
  <<“Phone No: “<<Student1.getGuardian().getPhone()<<endl
  
  <<“Address: “<<Student1.getGuardian().getAddress()<<endl;

 //Guardian Guardian1;
 //Guardian1.setFirstName(“Huang”);
 //Student1.setGuardian(Guardian1);
 //cout<<Student1.getGuardian().getFirstName()<<endl;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s