Creating Classes

Definition

class student{
	private:
		int ID;
		string name;
	public:
		void setName(string s);
		string getName();
		void print();
};

Implementation

#include <iostream>
using namespace std;
#include "student.h"

void student::setName(string s){
	name = s;
}

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

void student::print(){
	cout << "student name: " << name << endl;
	cout << "student ID: " << ID << endl;
}

Using the Class

#include "student.h"

int main(){
	student x, y;
	
	x.setName("Ayman");
	y.setName("Mohammad");
	
	x.print();
	y.print();

}

Constructors

class student{
	private:
		int ID;
		string name;
	public:
		student();
		void setName(string s);
		string getName();
		void print();
};
#include <iostream>
using namespace std;
#include "student.h"

student::student(){
	ID = 0;
	name = "";
}

void student::setName(string s){
	name = s;
}

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

void student::print(){
	cout << "student name: " << name << endl;
	cout << "student ID: " << ID << endl;
}