190 likes | 867 Views
Interface Implementation Separation in C++. Dr.D.Jeya Mala Associate Professor Dept.of Computer Applications Thiagarajar College of Engineering Madurai -15. Tamil Nadu, India. Introduction. Interface Contains only the abstractions in terms of method declarations. Implementation
E N D
Interface Implementation Separation in C++ Dr.D.Jeya Mala Associate Professor Dept.of Computer Applications Thiagarajar College of Engineering Madurai -15. Tamil Nadu, India. This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
Introduction • Interface • Contains only the abstractions in terms of method declarations. • Implementation • Contains the implementation part of the method declarations. • purposes: • Reusability • Extensibility This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
Interface Implementation Separation in C++ • Interface of Rectangle Class - Stored as Rect.h class Rectangle { public: Rectangle(); void setData(float, float); void calcArea(void); float getWidth(void); float getLength(void); float getArea(void); ~Rectangle(); private: float width, length, area; }; This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
Implementation of Rectangle Class – Rect.cpp Rectangle :: Rectangle() { width=0; length=0; } void Rectangle::setData(float w, float l) { width = w; length = l; } void Rectangle::calcArea(void) { area = width * length; } This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
Program continues float Rectangle::getWidth(void) { return width; } float Rectangle::getLength(void) { return length; } float Rectangle::getArea(void) { return area; } Rectangle::~Rectangle() { } This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
Main program – Creation of Objects and Inovoking class members void main(void) { Rectangle box; // instance creation float wide, long; cout << "This program will calculate the area of a\n"; cout << "rectangle. What is the width? "; cin >> wide; cout << "What is the length? "; cin >> long; box.setData(wide, long); box.calcArea(); cout << "Here is the rectangle's data:\n"; cout << "width: " << box.getWidth() << endl; cout << "length: " << box.getLength() << endl; cout << "area: " << box.getArea() << endl; } This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
Program Output This program will calculate the area of a rectangle. What is the width? 10 [Enter] What is the length? 5 [Enter] Here is the rectangle's data: width: 10 length: 5 area: 50 This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
Summary • The discussion focused on • OO Concepts • Creation of Classes and Objects • Implementation of Abstraction and Encapsulation • Interface and Implementation Separation in C++ This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
Quiz • A Class is a _____________ entity; whereas an Object is a __________ entity. • List the OO Concepts. • Elimination of inessential properties and grouping of essential ones is _________ • A Class contains __________ and _____________ • Binding of Member Data and Member Functions is ______________ • When object is created for a class _________ will be shared and _________ will be uniquely created. • Why interface and implementation separation is important? • Apply Abstraction and Encapsulation to a ‘student’ class that has attributes as regno, name which is a character array, marks in five subjects and methods to get, set and calculating grade with their interface and implementation separation. This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
Works Cited • BjarneStroustrup, “The C++ Programming Language”, 3rd Edition and Special Edition, Addison Wesley, 2000 • Robert Laffore, “Object Oriented Programming using C++”, 4th Edition, Sams Publishing, 2002. • Stanley Lippman, “C++ Primer”, 4th Edition, Pearson Education, 2007. • Yashavant P. Kanetkar, “Let Us C++”, BPB Publications, 2007. • D.Jeya Mala, S.Geetha, “Object Oriented Analysis and Design using UML”, Tata McGraw Hill Publishers, 2013. This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.