Uploaded by malikijaz5900307

tasks

advertisement
Namal University, Mianwali
Department of Electrical Engineering
Course Title: CS 101Computer Programming
Lab Report
Lab # 5
Title: Abstraction
Student Name
Muhammad Ijaz
Roll Number
NUM-BSEE-2022-01
Instructions
1. This is an individual lab. You will perform the lab and home tasks individually and
submit a report.
2. Some of these tasks (marked as ‘Example’) are for practice purposes only while others
(marked as ‘Task’) have to be answered in the report.
3. When asked to display an output in the task, take a screenshot, in order to insert it in the
report. Use system(“color f0”) instruction in main to produce output with white
background and black foreground.
4. The report should be submitted on the given template, including:
a. Code (copy and pasted, NOT a screenshot)
b. Output figure (as instructed in 3)
c. Explanation (2-3 Lines) must include the discussion on new concept.
5. The report should be properly formatted, with easy to read code and easy to see figures.
6. Plagiarism or any hint thereof will be dealt with strictly.Late submission of report is
allowed within 03 days after lab with 20% deduction of marks every day.
7. You have to submit report in pdf format (Reg.X_CP_LabReportX.pdf).
1
Task 1: Write a program having a base class Student with data members rollno, name and
Class define a member functions getdata() to input values and another function putdata() to
display all values. A class Test is derived from class Student with data members T1marks,
T2marks, T3marks, Sessional1, Sessional2, Assignment and Final. Also make a function
getmarks() to enter marks for all variables except Final and also make a function putmarks()
to display result. Make a function Finalresult() to calculate value for final variable using
other marks. Then display the student result along with student data.
Source Code:
#include <iostream>
#include <cstring>
using namespace std;
class student
{
private:
string name;
int roll;
public:
void getdata(string a,int b)
{
name=a;
roll=b;
}
void putdata()
{
cout<<"The name is :\n"<<name;
cout<<"\nThe roll number is:\n"<<roll;
}
};
class test:public student
{
private:
float marks1;
float marks2;
float marks3;
float sessional1;
float sessional2;
float assignment;
float final=36.5;
public:
void getmarks(float a,float b,float c,float d,float e,float f)
{
marks1=a;
marks2=b;
marks3=c;
sessional1=d;
sessional2=e;
assignment=f;
}
void putmarks()
{
cout<<"\nThe first subject marks are: "<<marks1;
2
cout<<"\nThe second subject marks are: "<<marks2;
cout<<"\nThe third subject marks are: "<<marks3;
cout<<"\nThe sessional 1 marks are: "<<sessional1;
cout<<"\nThe sessional 2 marks are: "<<sessional2;
cout<<"\nThe assignment marks are: "<<assignment;
}
void final1()
{
final=final+marks1+marks2+marks3+sessional1+sessional2+assignment;
cout<<"\nThe final marks are:"<<final;
}
};
int main()
{
string a;
cout<<"Enter the name of student:\n";
cin>> a;
test r;
r.getdata(a,01);
r.putdata();
r.getmarks(9,8.5,10,7.5,8,10);
r.putmarks();
r.final1();
return 0;
}
Output Screen Shot:
Explaination:
This is a C++ program that creates a class to store and display information about a student and their academic
performance.
The base class "student" has two private data members - name and roll - and two public member functions getdata() to set the values of name and roll, and putdata() to display the values of name and roll.
The derived class "test" inherits from the "student" class and adds five private data members to store marks for three
subjects, two sessionals, and an assignment. It also has three member functions - getmarks() to set the values of the
marks, putmarks() to display the values of the marks, and final1() to calculate and display the final marks.
In the main() function, an object of the "test" class is created, and the values for name, roll, and marks are set using
the member functions. The values of name, roll, and marks are then displayed using the member functions. Finally,
the final marks are calculated and displayed.
3
The program uses cout to display the output on the console, and cin to read input from the user.
The program demonstrates the use of classes, inheritance, data encapsulation, and member functions in C++.
Task 2: In previous examples, make a protected data item “Final” in base class. Assign the value
of this data item in member function Finalresult() of derived class.
Source Code:
#include <iostream>
#include <cstring>
using namespace std;
class student
{
protected:
float final;
private:
string name;
int roll;
public:
void getdata(string a,int b,float c)
{
name=a;
roll=b;
final=c;
}
void putdata()
{
cout<<"The name is :\n"<<name;
cout<<"\nThe roll number is:\n"<<roll;
}
};
class test:public student
{
private:
float marks1;
float marks2;
float marks3;
float sessional1;
float sessional2;
float assignment;
public:
void getmarks(float a,float b,float c,float d,float e,float f)
{
marks1=a;
marks2=b;
marks3=c;
sessional1=d;
sessional2=e;
assignment=f;
}
void putmarks()
{
cout<<"\nThe first subject marks are: "<<marks1;
cout<<"\nThe second subject marks are: "<<marks2;
4
cout<<"\nThe third subject marks are: "<<marks3;
cout<<"\nThe sessional 1 marks are: "<<sessional1;
cout<<"\nThe sessional 2 marks are: "<<sessional2;
cout<<"\nThe assignment marks are: "<<assignment;
}
void final1()
{
final=final+marks1+marks2+marks3+sessional1+sessional2+assignment;
cout<<"\nThe final marks are:"<<final;
}
};
int main()
{
string a;
float b;
cout<<"Enter the name of student:\n";
cin>> a;
cout<<"Enter the final marks of student out of 40:\n";
cin>>b;
test r;
r.getdata(a,01,b);
r.putdata();
r.getmarks(9,8.5,10,7.5,8,10);
r.putmarks();
r.final1();
return 0;
}
Output Screen Shot:
Explanation:
This is an updated version of the previous C++ program.
In this program, the "final" data member in the base class "student" is made protected, so that it can be accessed by
the derived class "test".
The main() function now also takes input from the user for the final marks of the student, and passes it to the base
class member function "getdata()".
The program then creates an object of the "test" class, and sets the values for name, roll, final marks, and subject
marks using the member functions.
The final marks are calculated and displayed using the member function "final1()".
5
The program demonstrates the use of access modifiers, protected data members, and inheritance in C++.
Task 3Create a food service program using a base class and two derived classes. The program should have a base class called
FoodService, which has the following member functions:
a)
serveFood(): prints a message to indicate that food is being served to customers.
b)
takeOrders(): prints a message to indicate that the food service is taking orders from customers.
c)
provideDiningArea(): prints a message to indicate that the food service provides a dining area for customers.
d)
calculateBill(): calculates the total bill based on the number of items ordered and the price of each item.
The program should also have two derived classes, DesiFoodService and FastFoodService, which derived from the
FoodService base class.
The DesiFoodService class should have the following additional member functions:
a) serveDesiFood(): prints a message to indicate that the food service is serving traditional South Asian cuisine.
b) provideDesiUtensils(): prints a message to indicate that the food service provides utensils suitable for Desi food.
c) calculateDesiBill(): calculates the total bill for Desi food based on the number of items ordered and the price of each
item. Consider price of each item as Rs. 100.
The FastFoodService class should have the following additional member functions:
a) serveFastFood(): prints a message to indicate that the food service is serving quick and convenient food options.
b) useFastFoodPackaging(): prints a message to indicate that the food service is using fast food branding and packaging.
d) calculateFastFoodBill(): calculates the total bill for fast food based on the number of items ordered and the price of
each item. Consider price of each item as Rs. 200.
In the main() function of the program, create objects of both derived classes and use their member functions to serve food,
take orders, and calculate bills for Desi food and fast food.
Finally, calculate and print the total bill for both types of food.
Source Code:
#include <iostream>
#include <cstring>
using namespace std;
class foodservice
{
public:
void servefood()
{
cout<<"The food is being served to customers\n";
}
void takeorder()
{
cout<<"The food service is taking orders from customers\n";
}
void provideDiningArea()
{
cout<<"The food service provides a dining area for customers\n";
6
}
};
class desiservice:public foodservice
{
public:
void serveDesiFood()
{
cout<<"The food service is serving traditional SouthAsian cuisine\n";
}
void provideDesiUtensils()
{
cout<<"The food service provides utensils suitable for Desi food.\n";
}
void calculateDesiBill(int a)
{
a=a*100;
cout<<"The Desi food bill is:"<<a;
}
};
class fastservice:public foodservice
{
public:
void serveFastFood()
{
cout<<"\n\nThe food service is serving quick and convenient food options\n";
}
void useFastFoodPackaging()
{
cout<<"The food service is using fast food branding and packaging\n";
}
void calculateFastFoodBill(int a)
{
a=a*200;
cout<<"The fast food bill is:"<<a;
}
};
int main()
{
int a,b,c;
cout<<"Enter the quantity of desi food:\n";
cin>>a;
cout<<"Enter the quantity of fast food:\n";
cin>>b;
desiservice f1;
fastservice f2;
f1.servefood();
f1.takeorder();
f1.serveDesiFood();
f1.provideDiningArea();
f1.provideDesiUtensils();
f1.calculateDesiBill(a);
f2.serveFastFood();
f2.provideDiningArea();
f2.useFastFoodPackaging();
f2.calculateFastFoodBill(b);
7
cout<<"\nThe overall bill is:\n"<<a*100+b*200;
return 0;
}
Output Screen Shot:
Explanation:
This program demonstrates the use of inheritance in a food service system. It defines a base class "foodservice"
and two derived classes "desiservice" and "fastservice". The derived classes inherit the properties of the base class
and add their own unique features. The program takes input from the user regarding the quantity of desi and fast
food they want to order and calculates the total bill accordingly. The program outputs the details of the food
service being provided to the customers, including the type of food being served, the utensils being used, and the
bill amount.
Task 4: Modify your program in Task 4 by adding a protected data member in the base class
FoodService that represents the total bill, and update it in both the derived classes whenever a
bill is calculated. In the main() function, print the total bill for each type of food and the overall
total bill.
Source Code:
#include <iostream>
#include <cstring>
using namespace std;
class foodservice
{
protected:
int sumall;
public:
void servefood()
8
{
cout<<"The food is being served to customers\n";
}
void takeorder()
{
cout<<"The food service is taking orders from customers\n";
}
void provideDiningArea()
{
cout<<"The food service provides a dining area for customers\n";
}
void totalbill(int a,int b)
{
sumall=a*100+b*200;
cout<<"the overall bill is:\n"<<sumall;
}
};
class desiservice:public foodservice
{
public:
void serveDesiFood()
{
cout<<"The food service is serving traditional SouthAsian cuisine\n";
}
void provideDesiUtensils()
{
cout<<"The food service provides utensils suitable for Desi food.\n";
}
void calculateDesiBill(int a)
{
a=a*100;
cout<<"The Desi food bill is:"<<a;
}
};
class fastservice:public foodservice
{
public:
void serveFastFood()
{
cout<<"\n\nThe food service is serving quick and convenient food options\n";
}
void useFastFoodPackaging()
{
cout<<"The food service is using fast food branding and packaging\n";
}
void calculateFastFoodBill(int a)
{
a=a*200;
cout<<"The fast food bill is:"<<a;
}
};
int main()
{
int a,b,c;
cout<<"Enter the quantity of desi food:\n";
9
cin>>a;
cout<<"Enter the quantity of fast food:\n";
cin>>b;
desiservice f1;
fastservice f2;
f1.servefood();
f1.takeorder();
f1.serveDesiFood();
f1.provideDiningArea();
f1.provideDesiUtensils();
f1.calculateDesiBill(a);
f2.serveFastFood();
f2.provideDiningArea();
f2.useFastFoodPackaging();
f2.calculateFastFoodBill(b);
f1.totalbill(a,b);
return 0;
}
Output Screen Shot:
Explanation
This program is an updated version of the previous one. It includes a new member function called "totalbill" in the
base class "foodservice", which calculates the overall bill by adding up the individual bills of both the "desiservice"
and "fastservice" objects. The totalbill function uses protected member variable "sumall" to store and output the
total amount. The program takes user input for the quantity of desi and fast food and creates respective objects.
The member functions of both derived classes are then called to serve food, take orders, calculate bills, etc.
Finally, the totalbill function is called to calculate the overall bill.
10
CP Lab Rubrics
 Methods of Evaluation: In the Lab : Implementation along with viva, Lab report submitted on QOBE.
 Measured Learning Outcomes :
CLO 1: Develop fundamental programming concepts and logic building through the practice exercises and revision of basic programming principles.
CLO 3: Present concise yet comprehensive technical reports.
Assessment tool /
weightage
(CLO, PLO)
Excellent
10
Good
9-7
Satisfactory
6-4
Unsatisfactory
3-1
Poor
0
Implementation
(35%)
(CLO1, PLO4)
Correct codes with
proper indentation
and comments.
Output provide all
required test cases.
Mostly correct codes
with proper
indentation and
comments. Outputs
provided covers most
of the required test
cases.
Some correct codes
with partially correct
indentation and
comments. Outputs
provided misses
some of the required
test cases.
Mostly incorrect
codes, with out
proper indentation
and comments or not
submitted. Outputs
provided misses
most of the required
test cases.
All codes and
outputs were
incorrect or not
submitted.
Viva (35%)
(CLO1, PLO4)
All the asked
questions during viva
are answered
correctly.
One of the asked
question is not
answered correctly.
Two of the asked
questions are not
answered correctly.
Three of the asked
questions are not
answered correctly.
As no code is
submitted, the viva is
not applicable.
Report (30%)
(CLO3, PL10)
Report with major
Report with proper
Report with minor
formatting errors.
formatting. A proper formatting errors. A Explanations of each
explanation of the
proper explanation of
code are not
code is provided with the code is provided
satisfactory. The
correct output.
with correct output. report include correct
output.
Report with major
formatting errors.
Explanations of each
code are not
satisfactory. The
report include some
incorrect output.
Report not
submitted.
Total
Marks
11
Download