










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This lab exercise focuses on object-oriented programming concepts, specifically exploring classes and objects in c++. It covers topics like constructor overloading, defining member functions outside the class, passing objects as arguments, utilizing the default copy constructor, and returning objects from member functions. The lab provides practical examples and code snippets to illustrate these concepts.
Typology: Lecture notes
1 / 18
This page cannot be seen from the preview
Don't miss anything!











Document History Rev. Date Comment Author 1.0 22/10/2023 Initial draft Dr. Naureen Shaukat
1. 1 06 / 10 /202 4 Modified Engr. Majid Ali Instructions
Syntax:
the interface from the implementation, promoting better organization and modularity in your code. Steps:
public: student():name("unknown"),rollno(0),marks(0.0) // Default Constructor { } student(string n,int ro,double m) // parameterized constructor { name=n; rollno=ro; marks=m; } char calculateGrade() // Member Function { char grade; if(marks>=90) { grade='A'; return grade; } else if (marks>=80) { grade='B'; return grade; } else if (marks>=70) { grade='C'; return grade; } else if (marks>=60)
{ grade='D'; return grade; } else if (marks>=50) { grade='E'; return grade; } } void DisplayStudentdeatails() // Member Function { cout<<"Student Name " << name <<endl; cout<<"Roll Number " << rollno << endl; cout<<"Marks"<< marks <<endl; cout<<"Grades"<<calculateGrade()<<endl; } }; int main() // Main Function { student s1("Nouman",23,90);// Object Created s1.DisplayStudentdeatails(); // Function Called return 0; } Output : Student Name: Nouman Roll Number: 23 Marks: 90 Grades: A
Code: ▪ Main.CPP #include"Circle.h" #include
void Circle::CompareArea(Circle c2 ){ if(c2.area()>area()) { cout<<"Circle 2 radius is greather than Circle 1 Radius ( True )"<<endl; } else { cout<<" False "<<endl; } } ▪ Circle.h #ifndef CIRCLE_H #define CIRCLE_H class Circle { private: double radius; public: Circle(double r); double area(); void CompareArea( Circle c2); }; #endif // CIRCLE_H Output: Circle 2 radius is greather than Circle 1 Radius ( True ) Explanation: In this task, I created a Circle class with a private member radius and a parameterized constructor for initialization. The class includes two member functions: calculateArea, which computes the area using the formula Area=πradiusradius, and compareAreas, which compares the areas of two Circle objects. In the main function, I created two Circle objects and used the default copy constructor to create a third object after this I compared the areas of the circles and displayed the results by using compareAreas Function. I created a Header file and include circle.h, circle.cpp and arrange code according to the header file
// Member Fucntion { double total=0; double sum=0; sum=marks+a2.getmarks()+a3.getmarks()+a4.getmarks()+a5.getmarks()+a6.getmarks()+a7.getmark s()+a8.getmarks()+a9.getmarks()+a10.getmarks(); total=sum/10; cout<<"The Average of Ten marks were " << total<<endl; } }; int main() { marks_calculation a1(55); marks_calculation a2(55); marks_calculation a3(55); marks_calculation a4(55); marks_calculation a5(55); a2=a1; a3=a2; a4=a3; a5=a4; marks_calculation a6(90); marks_calculation a7(80); marks_calculation a8(75); marks_calculation a9(70); marks_calculation a10(65); a1.calculate_averagemarks(a2,a3,a4,a5,a6,a7,a8,a9,a10);
return 0; } Output: The Average of Ten marks were 65. Explanation: In this task, I created a marks calculation class with a marks data member, initialized via a parameterized constructor. The calculate_averagemarks function calculates the average marks across ten marks calculation objects by summing their marks values, then dividing by 10. In main , ten objects (a1 to a10) are created, and calculate_averagemarks is called on a1 to compute and display the average of all ten marks. Home Task: Header File Include class as a header file
marks_calculation a8, marks_calculation a9, marks_calculation a10) { double total = 0; double sum = marks + a2.getmarks() + a3.getmarks() + a4.getmarks() + a5.getmarks() + a6.getmarks() + a7.getmarks() + a8.getmarks() + a9.getmarks() + a10.getmarks(); total = sum / 10; cout << "The Average of Ten marks is " << total << endl; } marks_calculation marks_calculation::total_marks(marks_calculation a2, marks_calculation a3, marks_calculation a4, marks_calculation a5, marks_calculation a6, marks_calculation a7, marks_calculation a8, marks_calculation a9, marks_calculation a10) { double sum = marks + a2.getmarks() + a3.getmarks() + a4.getmarks() + a5.getmarks() + a6.getmarks() + a7.getmarks() + a8.getmarks() + a9.getmarks() + a10.getmarks(); return marks_calculation(sum); } ▪ main.cpp #include "marks_calculation.h" int main() { marks_calculation a1(55); marks_calculation a2(55); marks_calculation a3(55); marks_calculation a4(55); marks_calculation a5(55);
a2 = a1; a3 = a2; a4 = a3; a5 = a4; marks_calculation a6(90); marks_calculation a7(80); marks_calculation a8(75); marks_calculation a9(70); marks_calculation a10(65); a1.calculate_averagemarks(a2, a3, a4, a5, a6, a7, a8, a9, a10); marks_calculation total = a1.total_marks(a2, a3, a4, a5, a6, a7, a8, a9, a10); cout << "Total Marks: " << total.getmarks() << endl; return 0; } Output: The Average of Ten marks were 65. The Total Marks of the Subjects were 655 Explanation: In this task, I created a class named marks calculation with a private member mark to represent a student's marks. The class features a parameterized constructor for initialization and two member functions: calculate_averagemarks , which computes and displays the average of marks from nine other marks calculation objects, and total marks , which returns a new object containing the total marks. In the main function , I instantiated ten objects, and used the functions to calculate and display the average and total marks. After doing this, I created a header file and include marks_calculation.h and marks calculation.cpp, then I arranged the code accordingly and check whether my program is running and display the corresponding result.