You are on page 1of 6

OOPS using C++ Assignment No: 01

1. Consider you have to find out average marks of 100 students in 3rd semester BSc-IT.
Write simple program(s) using different iteration statements to accomplish it.

Ans. Using while loop:-

#include<iostream.h>
#include<conio.h>
void main ()
{
int i=1, mark, sum, avg;
Sum=0;
clrscr();
While(i<=100)
{
cout<<”Enter your students marks\t”;
cin>>mark;
sum=sum+mark;
i++;
}
avg=sum/100;
cout<<”The average mark of 100 students is “<<avg;
getch();
}

Using For loop:-

#include<iostream.h>
#include<conio.h>
void main ()
{
int i=1, mark, sum, avg;
Sum=0;
clrscr();
for(i=1;i<=10;i++)
{
cout<<”Enter your students marks\t”;
cin>>mark;
sum=sum+mark;
}
avg=sum/100;
cout<<”The average mark of 100 students is “<<avg;
getch();
}
2. Explain the functions:
strlen ( )
strrev ( )
strcmp ( ) with simple programs

Ans. strlen() :- strlent function is used to find the length of the string. The syntax of the strlen
functions is strlen(string variable). Function returns the length of the string or the number of
characters in the sring which is an integer.
Example:
int len;
char str[10]=”hello”;
len=strlen(str);

strrev():- strrev function arranges the characters in the string variable in reverse order
except for the null character. The syntax of strrev function is strrev(string variable). It returns the
reversed string.
Example:
char str[10]=”Hello”;
cout<<strrev(str);

strcmp():- strcmp function is used to compare two strings. The syntax of the strcmp is
strcmp(string1, string2). Every character of string1 will be compared with corresponding character
of string2. The function returns an integer value accordingly:
If string1<string2, it returns value<0.
If string1= =string2, it returns value=0.
If string1>string2, it returns value>0.
Example:
str1[10]=”Hello”;
str2[10]=”Wellcome”;
cout<<strcmp(str1,str2);

3. Discuss features of scope variables with respect to following parameters:


 Visibility
 Lifetime
 Initialized to
 Purpose

Ans. The several types of variables cab are created in c++ which can be different in terms of life,
visibility and the way it is initialized. These are also known as storage class.
There are three types of storage class: Automatic, External and static variables. Automatic
variables are default in c++. All the variables we have created and used in programs are automatic
variables.
The summary of all three storage classes is shown below:
Automatic Static External
Visibility Function Function Program
Lifetime Function Program Program
Initialized to Junk value in memory Zero Zero
Purpose Variables used by Same as automatic but the Variables used by
single function value should be retained when several functions
function terminates.

4. Define a structure called SMU_students with following data elements:


 Name
 Age
 LC_code
 Reg_No with appropriate data types

Next define another structure called BSc-IT-students with following elements (codes of BSc
3rd semester subjects):
 BT0041
 BT0042
 BT0043
 BT0044
 BT0045 using appropriate data type

Declare a variable say James of type SMU_students and variable Sub_code of type
BSc_students in main( ) program. Write code to access individual subjects marks for any one
student.
Ans. #include<stdio.h>
#include<iostream.h>
Struct SMU_students
{
Name char[30];
Age int;
LC_Code int;
Reg_no long;
};
Struct BSc-IT-Students
{
BT0041 int;
BT0042 int;
BT0043 int;
BT0044 int;
BT0045 int;
};
Void main()
{
SMU_students James;
BSc-IT-Students Sub_code;
Cout<<”Enter Student detail as name, age, lc code, reg_no\n”;
Cin>>James.Name>> James.Age>> James.LC_Code>> James.Reg_no;
Cout<<”Enter marks statement\n”;
Cin>>Sub_code.BT0041>> Sub_code.BT0042>> Sub_code.BT0043>>
Sub_code.BT0044>> BT0045;
Cout<<”The Mark Statements of the students is\n”;
Cout<< James.Name<<endl;
Cout<< James.Age<<endl;
Cout<< James.LC_code<<endl;
Cout<< James.Reg_no<<endl;
Cout<< Sub_code.BT0041<<endl;
Cout<< Sub_code.BT0042<<endl;
Cout<< Sub_code.BT0043<<endl;
Cout<< Sub_code.BT0044<<endl;
Cout<< Sub_code.BT0045<<endl;
Getch();
}
OOPS using C++ Assignment No: 02

1. Define a class called distance with member elements Kilometers and Miles and a
member function called distance_finder( ).

Ans.
#include<iostream.h>
#include<conio.h>
class distance
{
Public :
Int Km, Miles;
void distance_finder()
{
Cout<<”Enter kilometers\t”:
Cin>>Km;
Cout<<”\nEnter kilometers\t”:
Cin>>Miles;
Cout<<”The distance is <<Mile<<” miles And “<<Km<<”kilometer”;
}
};
Void main()
{
clrscr();
Distance d;
d.distance_finder();
getch();
}
2. Define Inheritance. Implement two derived classes of distance called distance_meters
and distance_feet and write sample code for it.

Ans. Inheritence: Inheritence allows creating a class known as derived class from a class known
as base class and inheriting all the properties of the parent class which allows programs to be
reused without rewriting entire code. The members that can be inherited have to be declared using
protected access specifier. There can be several level of inheritance and the derived class can be
inherited from multiple parents as well.
#include<iostream.h>
#include<conio.h>
Class distance
{
KM int;
Meters int;
Feet int;
Public distance()
{
Cout<<”Enter distance in km, meter and feet\n;
Cin>>KM>>Meters>>Feet;
}
};
Class distance_meter: public distance
{
int a,b;
a=KM;
b=Meters;
};
Class distance_feet: public distance
{
int c;
c=Feet;
};
Void main()
{
distance d;
}

3. Explain Public, Private and Protected access specifiers with an example program

Ans. Public: It allows the variables to accessible within the class as well as outside of the class.
Private: This type of variable is accessible within the class only. We can not access private
data member of this class outside the class directly.

Protected: It works as a public class in the same class.

4. Define multiple inheritance. With a simple block diagram explain diamond inheritance.

Ans. Multiple Inheritence: A class can inherit the attributes of two or more classes in known as
multiple inheritence. Multiple inheritence allows us to combine the features of several existing
classes as a starting poing for defining new classes. It is like a child inheriting the physical features
of one parent and the intelligence of another.

Student Employee

Manager

You might also like