Virtual University VU Solved Assignments/Quizzes/GDBs, Past Solved Papers/Assignments/Online Quizzes Solutions.

Wednesday, June 24, 2009

CS201- Introduction to Programming Solution

Problem Statement:

Write a c++ program which consists of two classes:

1. Date

2. Student

First class Date should contain three data members:

Day

Month

Year

Date also contain following member functions:

Setter function

Getter function

showdate()



Now second class Student should have following three data members:

ST_ID

Name

Ad_date

Where ID and Name are char pointer while Ad_date (Date of Admission) is of type Date.



Student class should contain two member functions:

Display()

setdate()

Now in main function create an object of Class Student and call the member functions with it.

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

Sample output:



Name: Ali

ID: BC020400000

Admission Date: 12-08-2008



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





__________________________________________



SOLUTION:



// header file section


#include

#include

using namespace std;



class Date

{

public: int day;

int month;

int year;



public://Constructor

Date()

{

day=0;

month=0;

year=0;

}

//Member function declaration

//Mutator functions

void setDay(int);

void setMonth(int);

void setYear(int);

//Accessor functions

int getDate();

int getMonth();

int getYear();

//show date function

void showDate();

}

//member function definitions



void Date::setDate(int d)

{

//input validation

if (d<1>31)

{

cout<<"Invalid month please reenter it";

cin>>d;

}//end if

day=d;

}//end set day



void Date::setMonth(int m)

{

//input validation

if(m<1>12)

{

cout<<"invalid month please reenter it";

cin>>m;

}//end if

month=m;

}//end set month



void Date::setYear(int y)

{

year=y;

}//end set year



int Date::getDay()

{

return day;

}//end get day



int Date::getMonth()

{

return month;

}//end get month



int Date::getYear()

{

return year;

}//end get year



void Date:: showDate()

{

cout<

}

class Student

{

public:

char *ST_ID;

char *Name;

Date Ad_date;

public:

student()

{

Name=new char[20];

ST_ID=new char[10];

cout<<"Enter name:";

cin.getline(Name,20);

cout<<"Enter ID:";

cin.getline(ST_ID,10);

}

void setdate()

{

cout<<"Enter Day:";

cin>>Ad_date.day;

cout<<"Enter Month:";

cin>>Ad_date.month;

cout<<"Enter Year:";

cin>>Ad_date.year;

}

void Display()

{

cout<<"Name:"<

cout<<"ID:"<

cout<<"Admission Date:";

Ad_date.showDate();

}

};

main()

{

object.setDate();

object.display();

//pause system for a while

system("pause");

}//end main

No comments:

Post a Comment