🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

ScenicDarmstadtium1178

Uploaded by ScenicDarmstadtium1178

Tags

C++ programming object oriented concepts classes and objects

Full Transcript

IT1050-Object Oriented Concepts Lecture-06 - Classes in C++ 1 IT1050| Object Oriented Concepts| Lecture-06| Learning Outcomes At the end of the Lecture students should be able to Have a better understanding of the dif...

IT1050-Object Oriented Concepts Lecture-06 - Classes in C++ 1 IT1050| Object Oriented Concepts| Lecture-06| Learning Outcomes At the end of the Lecture students should be able to Have a better understanding of the differences between classes and objects (in C++ coding) Use setters and getters in a Class Write Object Oriented Programs Use header files with classes 2 IT1050| Object Oriented Concepts| Lecture-06| Classes and Objects class House { private: int length; House int width; - length int height - width int area; - height.. - area public: void paint(); + paint() … } Class House Blue Print of a House 3 IT1050| Object Oriented Concepts| Lecture-06| Classes and Objects Class Objects class House { private: myHouse1 int length; int main() { int width; House myHouse1; int height int area;.. public: void paint(); } … } 4 IT1050| Object Oriented Concepts| Lecture-06| Classes and Objects Objects Class class House { private: myHouse1 myHouse2 int length; int main() { int width; House myHouse1; int height House myHouse2; int area;.. public: void paint(); } … } 5 IT1050| Object Oriented Concepts| Lecture-06| Classes and Objects Objects Class class House { private: myHouse1 myHouse2 int length; int main() { int width; House myHouse1; int height House myHouse2; int area; myHouse1.paint(green);.. public: void paint(); } … } 6 IT1050| Object Oriented Concepts| Lecture-06| Classes and Objects Class Objects class House { private: myHouse1 myHouse2 int length; int width; int main() { int height House myHouse1; int area; House myHouse2;.. myHouse1.paint(green); public: myHouse2.paint(blue); void paint(); … } } 7 IT1050| Object Oriented Concepts| Lecture-06| Classes and Objects Class Objects class House { private: myHouse1 myHouse2 int length; int main() { int width; These attributes are protected House myHouse1; int height We can’t access these House myHouse2; int area; in the main function() myHouse1.paint(green);.. myHouse2.paint(blue); public: We interact with objects using void paint(); The public methods. We use the dot Operator to … } access methods. } 8 IT1050| Object Oriented Concepts| Lecture-06| Rectangle Class- private & public class Rectangle { private: Rectangle int width; int length; - length public: - width void setWidth(int w); + setWidth () int getWidth(); + getWidth() void setLength(int l); + setLength () int getLength() + getLength() int calcArea(); + calcArea () }; The diagram on the left side is a UML (Unified Modeling Language) Class Diagram. Here – means private and + means public 9 IT1050| Object Oriented Concepts| Lecture-06| Rectangle Class class Rectangle { private: int width; Rectangle int length; - length public: - width void setWidth(int w); A Setter int getWidth(); + setWidth () void setLength(int l); A Getter + getWidth() int getLength() + setLength () int calcArea(); + getLength() }; + calcArea () Since the properties length and width are protected and cannot be accessed from the main function we usually write two methods per property. A set method (setters) IT1050| Object Orientedand a get Concepts| method (getters). Lecture-06| 10 Setters (Mutators) class Rectangle { private: // A Setter starts with the word set int width; int length; // followed by the name of the property public: void setWidth(int w); // e.g. setWidth() int getWidth(); void setLength(int l); // setters are always methods that don’t int getLength() int calcArea(); // return values (void functions) }; We can do validations void Rectangle::setWidth(int w) { In a setter Here we are assuming that the if (w > 0) default width is 10. width = w; We know a Rectangle’s width cannot be zero or negative. else If someone sets a negative width width = 10; The Rectangle width will be set to 10. } 11 IT1050| Object Oriented Concepts| Lecture-06| Getters (Accessors) class Rectangle { private: int width; int length; public: // A Getter starts with the word get void setWidth(int w); int getWidth(); // followed by the name of the proeprty void setLength(int l); int getLength() // e.g. getWidth() }; int calcArea(); // getters always have the return type of // the property. Getters always contain the Following code. int Rectangle::getWidth(){ return property; return width; } 12 IT1050| Object Oriented Concepts| Lecture-06| Activity - 1 Item - itemCode - Name - price Write a class for Item and + setItemDetails() implement the setters and + setPrice() getters for the class. + getItemCode() + getPrice() 13 IT1050| Object Oriented Concepts| Lecture-06| Activity – 1 – Class definition Item class Item { - itemCode private : - name int itemCode; - price char name; float price; + setItemDetails() public: + setPrice() + getItemCode() void setItemDetails(int no, char pName[]); + getPrice() void setPrice(float pPrice); int getItemCode(); float getPrice(); }; 14 IT1050| Object Oriented Concepts| Lecture-06| Setters of the item class void Item::setItemDetails(int no, char pName[]) { itemCode = no; strcpy(name, pName); } void Item::setPrice(float pPrice) { price= pPrice; } 15 IT1050| Object Oriented Concepts| Lecture-06| Getters of the item class int Item::getItemCode() { return itemCode; } float Item::getPrice() { return price; } 16 IT1050| Object Oriented Concepts| Lecture-06| Activity- 2 Write a client program to input the itemCode, name and price of an item and print the itemCode and price. IT1050| Object Oriented Concepts| Lecture-06| #include Activity-2 – #include using namespace std; Client Program int main() { Item itm1; int i_code; char i_name; float i_price; cout > i_code; cout > i_name; cout > i_price; itm1.setItemDetails(i_code, i_name); itm1.setPrice(i_price); cout

Use Quizgecko on...
Browser
Browser