Monday, May 5, 2008

Using class in c++

Define a class Teacher with the following specifications :

Private members :

Name                          20 characters

Subject                       10 characters

Basic, DA, HRA           float

Salary float

Calculate ( )               function computes the salary and returns it.

Public members :

Readdata ( )               function accepts the data values and invokes the calculate function.

Displaydata ( )            function prints the data on the screen.


PROGRAM:

#include <iostream.h>

#include <conio.h>

#include <stdio.h>

class student {

private:

char name[20];

char subject[10];

float basic;

float da;

float hra;

float salary;

float calculate();

public:

void readdata();

void displaydata();

};

float student::calculate() {

return basic+da+hra; 

}

void student::readdata() {

cout<<"\nEnter your name: ";

gets(name);

cout<<"\nEnter your basic salary: ";

cin>>basic;

cout<<"\nEnter your daily allowance: ";

cin>>da;

cout<<"\nEnter your Hourse Rent Allowance: ";

cin>>hra;

salary=calculate();

}

void student::displaydata() {

cout<<"\nYour name: "<<name;

cout<<"\nYour total salary: "<<calculate();

}

void main() {

clrscr();

student s1;

s1.readdata();

s1.displaydata();

getch();

}