Sunteți pe pagina 1din 6

Session 3

Instructor Inputs
1.2 Installing Windows XP Professional NIIT
Solutions to Exercises
This session includes exercises for Chapter 2.

Chapter 2

Exercise 1
You have defined an Employee class as part of developing a software application for Diaz
Telecommunications Inc. The class, which you’ve defined, is as follows:
class Employee
{
char firstName;
char lastName;
char dateOfBirth;
char dateOfJoining;
char city;
char phoneNo;
public:
void accept()
{
//Code to accept employee details
}
void display()
{
//Code to display employee details
}
}

The Employee class definition shown above contains errors and is incomplete. Identify
and fix the errors. Complete the code in the Employee class definition and write the
main() function. Compile and execute the code.

Solution
The solution for this exercise is given in the TIRM CD:

DATAFILES FOR FACULTY\ Programming Using C++\2.U.1\2U1.cc


Exercise 2
Jim works for an airlines company. Customers fill the Booking Request Form with the
details of the flight like the flight number, destination, date and hand it over to Jim. He
then books tickets for the customers based on the availability of seats.

Identify the classes and objects involved in the above scenario and their attributes. Write
methods in the class to accept and display the values of the different attributes.

Solution
The solution for this exercise is given in the TIRM CD:

DATAFILES FOR FACULTY\ Programming Using C++\2.U.2\2U2.cc

Exercise 3
Predict the output of the following code:
#include <iostream>
void main()
{
char ch=’A’;
char ch1=66;
int i=60.00;
cout<<ch<<endl<<ch1<<endl<<i;
}

Solution
The output of the preceding code will be:

60

3.4 Instructor Inputs NIIT


Exercise 4
Predict the output of the following code:
#include <iostream>
class Customer
{
public:
int age;
};
void main()
{
Customer obj1, obj2;
cout<<”Enter the first customer’s age:”;
cin>>obj1.age;
obj2=obj1;
cout<<obj1.age<<” is the age of customer1”<<endl;
cout<<obj2.age<<” is the age of customer2”<<endl;
}

Solution
The output of the preceding code if the user has entered 12 will be:

12 is the age of customer1

12 is the age of customer2

Additional Exercises

Exercise 1
The following program should interchange the values of two variables var1 and var2 and
should print the new values of the variables. However, the program does not generate the
desired output. Identify the error in the program and write the correct code:
#include<iostream>
class interchange
{
private:
int var1;
int var2;
int temp;
void swap()
{
var1=5;
var2=10;
temp=var1;
var1=var2;

NIIT Instructor Inputs 3.5


var2=var1;
}
void display()
{
cout<<"The new value of variable1 is:"<<var1<<endl;
cout<<"The new value of variable2 is:"<<var2<<endl;
}
};
void main()
{
interchange I1;
I1.swap();
I1.display();
}

Solution
The solution for this exercise is given in the TIRM CD:

DATAFILES FOR FACULTY\ Programming Using C++\2.A.1\2A1.cc

3.6 Instructor Inputs NIIT

S-ar putea să vă placă și