Docsity

Object-Oriented Programming (OOP) Lab-4: Classes and Objects II, Lecture notes of Object Oriented Programming

Partial preview of the text

Download Object-Oriented Programming (OOP) Lab-4: Classes and Objects II and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Namal University, Mianwali

Department of Electrical Engineering

CSC-101L- Object Oriented Programming (Lab)

Lab - 4

Classes and Objects II

Student’s Name Nouman Mukhtar

Roll No. NUM-BSEE- 2023 - 23

Date Performed 25 th^ October 2024 (Friday)

Marks Obtained

Course Instructor: Lab Instructor:

Dr. Naureen Shaukat Engr. Majid Ali

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

  • This is an individual lab. You will perform the lab and home tasks individually and submit a report.
  • Some of these tasks (marked as ‘Example’) are for practice purposes only while others (marked as ‘Task’) have to be answered in the report.
  • When asked to display an output in the task, take a screenshot, in order to insert it in the report.
  • The report should be submitted on the given template, including: o Code (copy and pasted, NOT a screenshot) o Output figure (as instructed in 3) o Explanation (2-3 Lines) must include the discussion on new concept.
  • The report should be properly formatted, with easy-to-read code and easy to see figures.
  • 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.
  • You have to submit report in pdf format (Reg.X_OOP_LabReportX.pdf).

Syntax:

  • Declaration inside the class.
  • Definition outside the class using the scope resolution operator (: :). User can pass an object as an argument to a member function , allowing that function to access and manipulate the object's data. User can pass an object as an argument to a member function , allowing the function to access and manipulate the data of that object. This facilitates interactions between objects of the same class. Syntax: class ClassName { public: void memberFunction(const ClassName &obj) { // Access obj's members } }; A default copy constructor is a special constructor that creates a new object as a copy of an existing object. It initializes the new object with the same values as the existing object. If a class does not explicitly define a copy constructor, the compiler provides a default one that performs a shallow copy of the object's members. Returning an object from a member function allows the function to create and return a new instance of the class to the caller (e.g., the main function). This can be useful for functions that need to produce a modified version of the current object or create a new object based on certain calculations Syntax: class ClassName { public: // Default copy constructor ClassName(const ClassName &obj) { // Copy the values of obj's members to the new object this->memberVariable1 = obj.memberVariable1; this->memberVariable2 = obj.memberVariable2; // Continue for all members... } }; A header file in C++ is used to declare the structure of classes, functions and variables that can be shared between multiple source files. By defining a class in a header file, you can separate

the interface from the implementation, promoting better organization and modularity in your code. Steps:

  1. Create a Header File (ClassName.h):
    • Declare the class, its member variables, constructors, and member functions.
  2. Create a Source File (ClassName.cpp):
    • Define the member functions declared in the header file.
  3. Include the Header File in the Main Program (main.cpp):
    • Use #include to include the header file and create instances of the class, calling its member functions. #ifndef and #define Commands: In C++, #ifndef (if not defined) and #define are used as header guards to prevent multiple inclusions of the same header file, avoiding redefinition errors in a program.
  • #ifndef : Checks if a unique macro (e.g., MY_HEADER_H) has already been defined. If it’s not defined, the code between #ifndef and #endif is included.
  • #define : Defines the macro, marking that this file has been processed, so future inclusions skip this file. Syntax: #ifndef MY_HEADER_H // If MY_HEADER_H is not defined #define MY_HEADER_H // Define MY_HEADER_H // Class or function definitions here #endif // End of the guard

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 using namespace std; int main() { Circle c1(55.90); Circle c2(60.90); c1.CompareArea(c2); Circle c3=c2; return 0; } ▪ Circle.Cpp #include "Circle.h" #include using namespace std; Circle::Circle(double r) { radius=r; } double Circle::area(){ return 3.1416radiusradius; }

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

  • Make Task.3 with class as a header file. Return Object from member function In the scenario presented in the previous Question3, add another object of class 'marks_calculations' that represents the total marks. Create another member function called 'total_marks' that takes ten objects as input arguments and returns an object to the main function, which will be stored in the total marks object. Finally, print the total marks. Code: ▪ marks_calculation.h #ifndef MARKS_CALCULATION_H #define MARKS_CALCULATION_H #include using namespace std; class marks_calculation { private: double marks;

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.