You are on page 1of 14

Tutorial I Answer

1 . The output would be as shown below. The program contains an endless loop.
9
4
1
0
-1
0
1
2
3
4
4
2
1
0
0 [endlessly]

2. The output would be as shown below.
4
1
1
0
0
22
1
0
0
1
1
3. The output would be as shown below.
4

4. The output would be as shown below.
46
1








Tutorial II Answer

1. The output would be as shown below, because when it is discovered that "!found" is true, the
second
half of the OR expression is not evaluated, and thus count is not decremented.
danger
count = 5


2. The output would be as shown below. The 'a' in "Titanic" has been changed to an 'i'.
Titinic
i

3. The output would be as shown below. The line in italics would be typed by the interactive
user.
Enter a sentence on the line below.
Please go away.
Please
The reason that only the word "Please" is picked up in the message array is that the extraction
operator >>
ignores leading white space characters and then reads non-white space characters up to the next
white space
character. That is, >> is "word oriented", not line oriented.



4 a. The output would be as shown below. The line in italics would be typed by the interactive
user.
Enter a sentence on the line below.
Please go away.
Please go away.

4 b. The output would be as shown below. The line in italics would be typed by the interactive
user.
Enter a sentence on the line below.
Please stop bothering me.
Please stop botherin
The reason that the entire line typed by the user is not picked up in the message array is that the
getline instruction will read at most 20 characters from the keyboard.




Tutorial III Answer

1. The conditional statement should be modified as follows:
if (income < 0.0)
cout << "You are going farther into debt every month." << endl;
else if (income < 1200.00)
cout << "You are living below the poverty line." << endl;
else if (income < 2500.00)
cout << "You are living in moderate comfort." << endl;
else
cout << "You are well off." << endl;



2. Using do...while... in this situation makes the code a little simpler.
int n;
do
{
cout << "Enter a non-negative integer: ";
cin >> n;
if (n < 0)
cout << "The integer you entered is negative." << endl;
}
while (n < 0);

3 . The output of the code fragment is as follows:
n = 4 k = 5









4. Types of functions: classified into 5 types
1. Function with no arguments and no return value.
#include<iostream>
using namespace std;
void display()
{ cout<<"Welcome to function";
}
int main()
{

display();
return 0;
}
Output:
Welcome to function

2. Function with arguments and no return value.
#include<iostream>
using namespace std;
void sum(int x, int y)
{
int z;
z=x+y; cout<<"sum="<<z<<endl;
}
int main()
{
sum(5,10);
return 0;
}
Output:
Sum=15

3. Function with arguments and return value.
#include<iostream>
using namespace std;
int sum(int x, int y)
{
return x+y;
}
int main()
{
int z;
z=sum(5,10);
cout<<"Sum="<<z<<endl;
return 0;
}
Output:
Sum=15

4. Function with no arguments and return value.
#include<iostream>
using namespace std;
int sum()
{
int x=5, y=10;
return x+y;
}
int main()
{
int z;
z=sum();
cout<<"Sum="<<z<<endl;
return 0;
}
Output:
Sum=15

5. Function that return multiple value.
#include<iostream>
using namespace std;
void sum( int x, int y, int *sum)
{
*sum=x+y;
}
int main()
{
int x=5, y=10 , z;
sum(x, y, &z);
cout<<"Sum="<<z<<endl;
return 0;
}
Output:
Sum=15










Tutorial IV Answer

1. Parameter passing method: classified into three types
1. Call from value (available only in C)

2. Call from address/pointer (available only in C)

#include<iostream>
using namespace std;
void swap(int x , int y)
{
int temp ;
temp = x ;
x = y ;
y = temp;
cout<<"After Swapping
are:"<<x<<endl<<y<<endl;
}
int main()
{
int a=10 , b=20 ;
cout<<"Before Swapping
are:"<<a<<endl<<b<<endl;
swap(a,b) ;
return 0;
}
#include<iostream>
using namespace std;
void swap(int *x , int *y)
{
int temp ;
temp = *x ;
*x = *y ;
*y = temp;
cout<<"After Swapping
are:"<<*x<<endl<<*y<<endl;
}
int main()
{
int a=10 , b=20 ;
cout<<"Before Swapping
are:"<<a<<endl<<b<<endl;
swap(&a,&b) ;
return 0;
}
Output:
Before Swapping are:10 20
After Swapping are:20 10
Output:
Before Swapping are:10 20
After Swapping are:20 10
In pass from value only the copy of actual
arguments is passed therefore the changes
made to formal arguments is not reflected back
to the original arguments.
In pass from Address, the address of actual
arguments is passed therefore the changes made
to formal arguments is also made to the actual
arguments.

3. Call from reference (available only in C++)
4. Default argument (available only in C++)
#include<iostream>
using namespace std;
void swap(int &x , int &y)
{
int temp ;
temp = x ;
x = y ;
y = temp;
cout<<"After Swapping are:"<<x<<endl<<y<<endl;
}
int main()
#include <iostream>
using namespace std;
int max(int num1, int num2=200)
{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
{
int a=10 , b=20 ;
cout<<"Before Swapping
are:"<<a<<endl<<b<<endl;
swap(a,b) ;
return 0;
}
int main ()
{
int a = 100;
int ret;
ret = max(a);
cout << "Max value is : " << ret << endl;
return 0;
}
Output:
Before Swapping are:10 20
After Swapping are:20 10
Output:
Max value is : 200

In pass from Reference, the reference of the actual
arguments is passed therefore the changes made to
formal arguments is also made to the actual
arguments.
In Default argument, the max method take 100 as an
argument assign to num1, another value num2=200 as
default value.


2. Access specifier (or) visibility label:

Access specifier private protected public
Visible to member
function same class
Yes Yes Yes
Visible to member
function derived class
No Yes Yes
Visible to member
function outside class
No No Yes
Data Security Very strong Moderate Weak

#include<iostream>
using namespace std;
class person
{
private:
int age;
char name[20];
void getdata()
{
cout<<"Enter age and
name:"<<endl;
cin>>age>>name;
}
void displaydata()
{

cout<<"Age="<<age<<endl;

cout<<"Name="<<name<<endl;
}
};
int main()
{
#include<iostream>
using namespace std;
class person
{
protected:
int age;
char name[20];
void getdata()
{
cout<<"Enter age and
name:"<<endl;
cin>>age>>name;
}
void displaydata()
{

cout<<"Age="<<age<<endl;

cout<<"Name="<<name<<endl;
}
};
int main()
{
#include<iostream>
using namespace std;
class person
{
public:
int age;
char name[20];
void getdata()
{
cout<<"Enter age and
name:"<<endl;
cin>>age>>name;
}
void displaydata()
{

cout<<"Age="<<age<<endl;

cout<<"Name="<<name<<endl;
}
};
int main()
{
person per1;
per1.getdata();
per1.displaydata();
return 0;
}
person per1;
per1.getdata();
per1.displaydata();
return 0;
}
person per1;
per1.getdata();
per1.displaydata();
return 0;
}
Output:
Compiler Error
Output:
Compiler Error
Output:
Enter age and name: 15
kumar
Age=15
Name=kumar


3. Special function available in C++
1. Inline function
The code replaces the function call with corresponding function code.
2. Friend function
C++ also allows us to access classs private and protected members
without a class member function. We can access that members using
friend function.
#include<iostream>
using namespace std;

inline int sum(int a, int b)
{
return a+b;
}
int main()
{
int x=10, y=20;
cout<<"Sum="<<sum(x,y);
return 0;
}
#include <iostream>
using namespace std;
class arithmetic
{
private:
int a; int b;
public:
void getdata(int,int);
friend int sum(arithmetic);
};
void arithmetic::getdata(int n1,int n2)
{
a=n1; b=n2;
}
int sum(arithmetic a1)
{
return a1.a+a1.b;
}
int main()
{
arithmetic obj;
obj.getdata(10,20);
cout << "Sum of numbers is = " << sum(obj)
<< "\n";
return 0;
}
Output:
Sum=30
Output:
Sum of numbers is = 30

4. Scope resolution operator: (::) serves two purposes
1. When local variable and global variable are having same name, local variable gets the
priority. C++ allows flexibility of accessing both the variables through a scope resolution
operator.
2. Use of Scope Resolution Operator to write function definition outside class definition.
#include<iostream>
using namespace std;
int x=10;
int main()
{
int x=20;
cout<<"Local value="<<x<<endl;
cout<<"Global value="<<::x<<endl;
return 0;
}
#include<iostream>
using namespace std;
class person
{
private:
int age;
char name[20];
public:
void getdata();
void displaydata();
};
void person :: getdata()
{
cout<<"Enter age and name:"<<endl;
cin>>age>>name;
}
void person :: displaydata()
{
cout<<"Age="<<age<<endl;
cout<<"Name="<<name<<endl;
}

int main()
{
person per1;
per1.getdata();
per1.displaydata();
return 0;
}
Output:
Local value=20
Global value=10


Output:
Enter age and name:
15
ramya
Age=15
Name=ramya





Tutorial V Answer




Tutorial VI Answer

1. Static data member and static member function:
A static member is shared by all objects of the class.
A static member function can only access static data member.
#include <iostream>
using namespace std;
class college
{
private:
int strollno;
string department;
static string collegename;
public:
void getdata(int rollno,string dept)
{
strollno=rollno;
department=dept;
}
void display()
{
cout<<"Rollno:"<<strollno<<endl;

cout<<"Department:"<<department<<endl;
cout<<"College
name:"<<collegename<<endl;
}
};class college new1[3];

string college :: collegename = "Sona college";
int main()
{
new1[0].getdata(01,"IT");
new1[1].getdata(02,"CSE");
new1[2].getdata(03,"ECE");
for(int i=0;i<3;i++)
{
new1[i].display();
}
return 0;
}

// Static data member with mamber function
#include <iostream>
using namespace std;
class student
{
private:

int m1, m2;
static double total;
public:
void getdata(int n1,int n2)
{
m1=n1;m2=n2;
total=m1+m2;
}
static void avg()
{
cout<<"Total:"<<total<<endl;
cout<<"Average:"<<total/2<<endl;
}
};class student st1;

double student :: total =0;
int main()
{
st1.getdata(10,20);
student :: avg();
return 0;
}
Output:
Rollno:1
Output:
Total:30
Department:IT
College name:Sona college
Rollno:2
Department:CSE
College name:Sona college
Rollno:3
Department:ECE
College name:Sona college

Average:15



2. Function overloading:
More than one function with same name, we can use same function name
for different purpose with different number and types of arguments.
#include<iostream>
using namespace std;
class maths
{
public:
void add(char a,char b) //Method-I
{
cout<<"Add two char values (ascii):"<<
a+b<<endl;
}
void add(int a,int b) //Method-II
{
cout<<"Add two integer values:"<< a+b<<endl;
}
void add( double a, double b) //Method-III
{
cout<<"Add two float values:"<< a+b<<endl;
}

void area(int a) //Method-IV
{
cout<<"Area of square:"<<a*a<<endl;
}
void area(double t,int a,int b) //Method-V
{
cout<<"Area of triangle:"<< t*a*b<<endl;
}
void area(double t,int a) //Method-VI
{
cout<<"Area of circle:"<< t*a*a<<endl;
}
}; class maths m1;
int main()
{
m1.add('a','b');
m1.add(10,20);
Output:
Add two char values (ascii):195
Add two integer values:30
Add two float values:31
Area of square:100
Area of triangle:100
Area of circle:314

m1.add(10.5,20.5);
m1.area(10);
m1.area(0.5,10,20);
m1.area(3.14,10);
return 0;
}
3. Object:
1. Array of object
2. Returning object

#include<iostream>
#include<cstring>
using namespace std;
class student
{
private:
int rollno;
string name;
string department;
public:
void getdata(int rno, string stname, string
stdept)
{
rollno = rno;
name = stname;
department = stdept;
}
void display()
{
cout<<"Roll number="<<rollno<<endl;
cout<<"Name="<<name<<endl;
cout<<"Department="<<department<<endl;
}
}; class student new1[3];

int main()
{
new1[0].getdata(01, "Suresh", "Information
Technology");
new1[1].getdata(02, "Siva", "Computer
Science");
new1[2].getdata(03, "Rani", "Civil
engineering");

for(int i=0;i<3;i++)
{
new1[i].display();
}
return 0;
}
#include<iostream>
using namespace std;
class student
{
private:
int m1,m2;
public:
void getdata(int x,int y)
{
m1 = x;
m2 = y;
}
void display()
{
cout<<"Marks in m1 = "<<m1<<endl;
cout<<"Marks in m2 = "<<m2<<endl;
}
student increased (student st1)
{
student st2;
st2.m1 = m1 + st1.m1;
st2.m2 = m2 + st1.m2;
return (st2);
}
}; class student s1,s2,s3;
int main()
{
s1.getdata(90,85);
s2.getdata(5,5);
s3 = s1.increased(s2);
s3.display();
return 0;
}
Output:
Roll number=1
Name=Suresh
Department=Information Technology
Roll number=2
Name=Siva
Department=Computer Science
Roll number=3
Name=Rani
Department=Civil engineering

Output:
Marks in m1 = 95
Marks in m2 = 90

You might also like