Document Details

EarnestDivergence

Uploaded by EarnestDivergence

Tags

C++ programming object-oriented programming classes and objects computer science

Full Transcript

IT1050- Object Orientation Lecture-05 Classes and Objects in C++ 1 Learning Outcomes At the end of the Lecture students should be able to; Implement a class and create objects using C++....

IT1050- Object Orientation Lecture-05 Classes and Objects in C++ 1 Learning Outcomes At the end of the Lecture students should be able to; Implement a class and create objects using C++. 2 IT1050| Object Oriented Concepts| Lecture 05 – Classes & Objects in C++| Recalling Steps in OOP Analyse the Problem OO Analysis and Identify Objects Design Develop Classes Create Objects OO Programming Build the Solution 3 IT1050| Object Oriented Concepts| Lecture 05 – Classes & Objects in C++| How to write an Object Oriented Program ? IT1050| Object Oriented Concepts| Lecture 05 – Classes & Objects in C++| Example-1 - Rectangle Class length Rectangle width Private length width setWidth() setLength() Public calcArea() 5 IT1050| Object Oriented Concepts| Lecture 05 – Classes & Objects in C++| Rectangle Class in C++ //C++ coding for Rectangle class Rectangle class Rectangle { width private: Private int width; length int length; setWidth() public: void setWidth(int no); setLength() Public void setLength(int no); calcArea() int calcArea(); }; 6 IT1050| Object Oriented Concepts| Lecture 05 – Classes & Objects in C++| Private & Public The private part of the definition specifies the properties (data members) of a class. These are hidden from outside the class and can only be accessed through the methods (operations/functions) defined for the class. The public part of the definition specifies the methods as function prototypes. These methods as they are called, can be accessed by the main program. IT1050| Object Oriented Concepts| Lecture 05 – Classes & Objects in C++| 7 Private & Public Rectangle private public setWidth() width Client setLength() length calcArea() 8 IT1050| Object Oriented Concepts| Lecture 05 – Classes & Objects in C++| Rectangle Class in C++ //C++ coding for Rectangle void Rectangle::setWidth(int no) { class Rectangle { width = no; private : } int width; void Rectangle::setLength(int no) { int length; length= no; public: } void setWidth(int no); int Rectangle::calcArea() { void setLength(int no); int area = length * width; int calcArea(); return area; } }; 9 IT1050| Object Oriented Concepts| Lecture 05 – Classes & Objects in C++| Student class – Activity 1 Student studentNo marks1 Implement the Student marks2 marks3 Private class in C++. setStudentNo() assignMarks () calcAvg() Public 10 IT1050| Object Oriented Concepts| Lecture 05 – Classes & Objects in C++| Answer : Student Class in C++ //C++ coding for Student class Student { private : int StudentNo; int marks1; int marks2; int marks3; public: void setStudentNo(int no); void assignMarks(int n1, int n2, int n3); float calcAvg(); }; 11 IT1050| Object Oriented Concepts| Lecture 05 – Classes & Objects in C++| void Student::setStudentNo(int no) { width = no; } void Student :: assignMarks(int n1, int n2, int n3);{ marks1 = n1; marks2 = n2; marks3 = n3; } float Student ::calcAvg() { float average = (marks1+marks2+marks3)/3.0; return average; } IT1050| Object Oriented Concepts| Lecture 05 – Classes & Objects in C++| Creating Objects Class_name Object_name; e.g: Rectangle rect1; // single object Rectangle rect1, rect2; // multiple objects Rectangle rectangles; // array of objects Note : Use C++ rules for identifiers when naming objects 13 IT1050| Object Oriented Concepts| Lecture 05 – Classes & Objects in C++| Creating Objects Rectangle rec1, rec2; rec1 : Rectangle rec2 : Rectangle width = 10 width = 5 length = 20 length = 10 14 IT1050| Object Oriented Concepts| Lecture 05 – Classes & Objects in C++| Accessing Public Methods #include using namespace std; int main() { Rectangle rec1, rec2; rec1.setWidth(10); rec1.setLength(20); rec2.setWidth(5); rec2.setLength(10); cout

Use Quizgecko on...
Browser
Browser