0% found this document useful (0 votes)
30 views

Lab Report 284

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Lab Report 284

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

1. Suppose you are meeting with your friend in school reunion after a long time.

You don’t have the


number of your friend. Assume your friends name is Ritu. You have given your number and then your
friend also gives her number to you. After some time, she said I have another number keep that one also.
That’s my official number. I use it during my office hours. So then again in your phone you are saving
her number under the same contact. Now which OOP features resemblance the situation? Write a C++
program on it.

Flow Chart:

Algorithm:

Step 1. Start
Step 2. Create Two Classes named "PhoneNumber" And "PhoneNumber2".
Step 3. Create Two methods in class “PhoneNumber” namely “number()” and “Show()”.
Step 4. Class “PhoneNumber2” Inherits the class “PhoneNumber”.
Step 5. Override the methods “number()” and “Show()” in the class “PhoneNumber2”.
Step 6. Enter the numbers.
Step 7. Display the numbers.
Step 8. End.

Program:

#include <iostream>
using namespace std;

class phone
{
public:
string name;
string num1,num2;
void contact(string n, string number1, string number2)
{
name=n;
num1=number1;
num2=number2;
}
void show()
{
cout<<"Name: "<<name<<endl;
cout<<"Personal phone: "<<num1<<endl;
cout<<"Official phone: "<<num2<<endl;
}
};

int main()
{
phone p;
p.contact("Ritu","01567856678","013456789");
p.show();
}

Screenshot of the result:

2. Write a C++ program that reads a floating-point number. If the number is zero it prints "zero",
otherwise, print "positive" or "negative". Add "small" if the absolute value of the number is less than 1, or
"large" if it exceeds 1,000,000.

Test Data Input a number: -2534 Expected Output :


Negative

Flow Chart:
Algorithm:

Step-1. Start
Step-2. Enter the number.
Step-3. If number==0 then print “zero” else go to step 4.
Step-4. If number>0 then go to step 5 else go to step 6.
Step 5. If Absolute value of the number the number is less than 1 then print “Small Positive” else if the
absolute value exceeds 1000000 then print “Large Positive” else print “Positive
Step-6. If Absolute value of the number the number is less than 1 then print “Small Negative” else if the
absolute value exceeds 1000000 then print “Large Negative” else print “Negative”.
Step-7. End.

Program:

#include <iostream>
using namespace std;
int main()
{
float n;
cout <<"Input value: ";
cin >> n;

if (n > 0)
{
if (n < 1)
{
cout <<"Positive small number";
}
else if (n > 1000000)
{
cout<<"Positive large number";
}
else
{
cout<<"Positive number";
}
}
else if (n <= -1)
{
if (n <= -1 )
{
cout<<"Negative small number";
}
else if (n > -1000000)
{
cout<<"Negative large number";
}
else
{
cout<<"Negative number";
}
}
else
{
cout<<"Zero";
} }

Screenshot of the result:


3. Following is a statement:

C++ is an OOP language

In this sentence there are 8 vowels and 8 constants. Write a C++ program that will take a string as an
input and will count the number of vowels and constants in it.

Flow Chart:

Algorithm:

Step 1. Start
Step 2. Print a Statement “Enter The String”
Step 3. Take an input value as string
Step 4 Check is it match with (a,e,I,o,u, A,E, I, O, U ) or not
Step 5. If string match print Vowels
Step 6. If string do not match, Print Constant
Step 7. End.

Program:

#include <iostream>
using namespace std;

int main()
{
string s;
int vowels, consonants;

vowels = consonants = 0;

cout << "Enter a string: ";


getline(cin, s);

for(int i = 0; i < s.length(); ++i)


{
if(s[i]=='a' || s[i]=='e' || s[i]=='i' ||
s[i]=='o' || s[i]=='u' || s[i]=='A' ||
s[i]=='E' || s[i]=='I' || s[i]=='O' ||
s[i]=='U')
{
++vowels;
}
else if((s[i]>='a'&& s[i]<='z') || (s[i]>='A'&& s[i]<='Z'))
{
++consonants;
}
}

cout << "Vowels: " << vowels << endl;


cout << "Consonants: " << consonants << endl;

Screenshot of the result:


4. Suppose you have a savings account in Dhaka bank. In that account you have some transaction
record. Every month you get some deposit in your account and you also withdraw some money
monthly. After all of the transactions of the month only you get to know the total balance information
in your mail sent by the bank authority.

Now which OOP features resemblance the situation? Write a C++ program on it.

Flow Chart:

Algorithm:

Step 1. Start
Step 2. Create a Class named “Account”.
Step 3. Create a Private variable balance.
Step 4. Create Get and set Method for Accessing the private variable.
Step 5. Create object of the class.
Step 6. Display the output.
Step 7. End.
Program:

#include <iostream>
using namespace std;

class Dhakabank {
private:
float balance = 50000;
float deposit = 2000;
float withdraw = 5000;

public:
void total(){
float a,b;

a = balance + deposit;
b = a - withdraw;

cout << "Previous Balance= " << balance << endl;


cout << "Balance after Deposit= " << a << endl;
cout << "Balance after Withdraw= " << b << endl;
}
};

int main()
{
Dhakabank d;
d.total();
}

Screenshot of the result:


5. In this pandemic situation the HSC 2020 batch students got auto pass as there was no other way to
take the exam by endangering the lives of those students. There HSC result was generated from their
JSC and SSC. For calculating their GPA 25 % GPA was taken from JSC and 75% was taken from
SSC.

Now write a C++ program where you will have to take input of the marks of a student for the subject
English for both JSC and SSC and find out the HSC marks along with the grade of each subject
Where,
80-100: Grade A, 70-79: Grade B, 60-69: Grade C, 50-59: Grade D, less than 50 fail.
For example:
In English the student got 70 in JSC and 90 in SSC. So his/her marks in HSC for English will be
(70×25%)+(90×75%) =17.5+67.5 =85 in HSC. grade A.

Flow Chart:

Algorithm:

Step 1. Start
Step 2. Enter JSC Marks.
Step 3. Enter SSC Marks.
Step 4.Calculate HSC marks.
Step 5.Check marks. if(hsc>=80 && hsc<=100) then print “Your Grade is A” else go to step 6.
Step 6. Check marks. if(hsc>=70 && hsc<=79) then print “Your Grade is B” else go to step 7.
Step 7. Check marks. if(hsc>=60 && hsc<=69) then print “Your Grade is C” else go to step 8.
Step 8. Check marks. if(hsc>=50 && hsc<=59) then print “Your Grade is D” else go to step 8. Step 9.
Check marks. if(hsc<50) then print “Your Grade is F”.
Step 9. End.

Program:

#include <iostream>
using namespace std;

int main()
{
double j,s;
cout<<"Enter the mark for JSC and SSC: ";
cin>>j;
cin>>s;
double total;
total=(j*0.25) + (s*0.75);
cout<<total<<" in HSC"<<endl;

if(total>=0 && total<=100)


{
if(total>=80 && total<=100)
{
cout<<"grade A";
}

else if(total>=70 && total<=79)


{
cout<<"grade B";
}

else if(total>=60 && total<=69)


{
cout<<"grade C";
}

else if(total>=50 && total<=59)


{
cout<<"grade D";
}
else
cout<< "Fail";
}

Screenshot of the result:

6. A constructor in C++ is a special method that is automatically called when an object of a class
is created. A destructor function cleans up any resources allocated to an object. In a class there
are 3 constructors. To clean up the allocated resources create a destructor function.

Write a C++ program on the above concept where the Name of the Class will be your name. and
member variable should show your age and height.

Algorithm:

Step 1. Start
Step 2. Create a class called Taseen
Step 3. Initiate first constructor which only print “My age and height”
Step 4. Create second constructor which has one parameter and print “Height” .
Step 5. Create another constructor which has one parameter and print only “Age”
Step 6. Create a main function.
Step 7. Assign the value of age and height
Step 8. End.

Program:

#include <iostream>
using namespace std;
class Taseen{
private:
double height;
int age;

public:
Taseen(){
cout<<"My age and height: "<<endl;
}
Taseen(double x)
{
height=x;
cout<<"Height: "<<height<<endl;
}
Taseen(int x)
{
age=x;
cout<<"Age: "<<age<<endl;
}

~Taseen(){

};

int main()
{
Taseen ();
Taseen a(22);
Taseen h(5.5);
}

Screenshot of the result:


7. Write a C++ program to find all pairs of elements in an array whose sum is equal to a specified
number.

Algorithm:

Step 1. Start
Step 2. Declare an array.
Step 3. Set a specific number.
Step 4. Enter Array Elements.
Step 5. Check sum of the 2 elements.
Step 6.If summation of two elements is equal to specific number then print the elements along with the
sum else keep checking.
Step 7. End.

Flow Chart:
Program:

#include <iostream>
using namespace std;
int main()
{

int n;
cout<<"Enter array size: ";
cin>>n;

int arr[n];
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}

int sum;
cout<<"Enter sum value: ";
cin>>sum;

int s = sizeof(arr)/sizeof(arr[0]);

cout << "Array: ";


for (int i=0; i < s; i++)
cout << arr[i] <<" ";

int c = 0;
cout <<"\nPairs of elements and whose sum equal to "<<sum;

for (int i=0; i<s; i++)


for (int j=i+1; j<s; j++)
if (arr[i]+arr[j] == sum)
{
cout << "\n" << arr[i] << "+" << arr[j]<<"="<<sum;
c++;
}
return 0;
}

Screenshot of the result:

8. Suppose you are meeting with your friend in school reunion after a long time. You don’t have
the number of your friend. Assume your friends name is Ritu. You have given your number and
then your friend also gives her number to you. After some time, she said I have another number
keep that one also. That’s my official number. I use it during my office hours. So then again in
your phone you are saving her number under the same contact.

Now which OOP features resemblance the situation. Write a C++ program on it.

Flow Chart:
Algorithm:

Step 1. Start
Step 2. Create Two Classes named "PhoneNumber" And "PhoneNumber2".
Step 3. Create Two methods in class “PhoneNumber” namely “number()” and “Show()”.
Step 4. Class “PhoneNumber2” Inherits the class “PhoneNumber”.
Step 5. Override the methods “number()” and “Show()” in the class “PhoneNumber2”.
Step 6. Enter the numbers.
Step 7. Display the numbers.
Step 8. End.

Program:

#include <iostream>
using namespace std;

class phone
{
public:
string name;
string num1,num2;
void contact(string n, string number1, string number2)
{
name=n;
num1=number1;
num2=number2;
}
void show()
{
cout<<"Name: "<<name<<endl;
cout<<"Personal phone: "<<num1<<endl;
cout<<"Official phone: "<<num2<<endl;
}
};

int main()
{
phone p;
p.contact("Ritu","01611456654","01710814239");
p.show();
}

Screenshot of the result:

9. Write a program that accepts three numbers from the user and prints "increasing" if the numbers
are in increasing order, "decreasing" if the numbers are in decreasing order and "Neither increasing
nor decreasing order" otherwise. Go to the editor

Test Data
Input first number: 1524
Input second number: 2345
Input third number: 3321
Expected Output :Increasing order

Flow Chart:
Algorithm:

Step 1. Start
Step 2. Enter 3 numbers a, b and c.
Step 3. If (a<b && b<c) Then print “Increasing Order” else go to step 4.
Step 4. If (a>b && b>c) Then print “Decreasing Order” else go to step 5.
Step 5. Print “Neither Increasing nor Decreasing”.
Step 6. End.

Program:

#include <iostream>
using namespace std;

int main()
{
double x,y,z;
cout<<"Input first number: ";
cin>>x;
cout<<"Input second number: ";
cin>>y;
cout<<"Input third number: ";
cin>>z;
if (x < y && y < z)
{
cout<<"Increasing order";
}
else if (x > y && y > z)
{
cout<<"Decreasing order";
}
else
{
cout<<"Neither increasing or decreasing order";
}
}

Screenshot of the result:


11. Create a Class called class with a method called Batch.
a) Create another class called ClassOne. And create a method with the name SectionA and inherit
the Class ClassOne.

FlowChart:

Algorithm:

Step 1. Start
Step 2. Create four classes named Class, ClassOne, Classtwo and School.
Step 3.Define a method a named SectionA() in the class ClassOne.
Step 4. Class ClassOne inherits class Class.
Step 5. Define the method named SectionC() And SectionD() in the class ClassTwo.
Step 6. Class ClassTwo inherits class Class.
Step 7. Create objects of all classes in the main method of School class and access all the mothods.
Step 8. End.
Program:

#include <iostream>
using namespace std;

class TestClass{
public:
void Batch(){
cout<<"Batch is 201"<<endl;
}
};
class ClassOne:virtual public TestClass{
public:
void SectionA(){
cout<<"This is section A"<<endl;
}
};

class ClassTwo:virtual public TestClass{


public:
void SectionC(){
cout<<"This is section C"<<endl;
}

void SectionD(){
cout<<"This is section D"<<endl;
}
};

class School:public ClassOne,public ClassTwo{

};

int main(){
School ob;
TestClass ob1;
ClassOne ob2;
ClassTwo ob3;
ob1.Batch();
ob2.SectionA();
ob3.SectionC();
ob3.SectionD();

Screenshot of the result:

You might also like