Download presentation
Presentation is loading. Please wait.
Published byἩρακλῆς Αντωνόπουλος Modified over 6 years ago
1
1.4 Abstract Data Types, Interfaces, and Pre- and Postconditions
1.3 Defining C++ Classes 1.4 Abstract Data Types, Interfaces, and Pre- and Postconditions 07 – Software Design
2
Attendance Quiz #6 Software Design
3
Tip #7: The Rule of Seven Software Design There is a rule of nature commonly known as the 'rule of seven'. This appears in grouping situations, where control is optimized around the seven mark. It appears in a wide number of situations, from the grouping of leaves on plants, to the number of layers in the OSI network model, to the number of layers of management in the Catholic church, to the number of chunks of information that the short-term memory can comfortably handle. A simple application of the rule of seven in programming is where there might be seven chunks of code in a function, each containing seven statements, thus giving a reasonable function size of around 49 lines. The actual grouping count in many situations is actually rather inexact, and an extension to this rule is 'seven, plus or minus two'. Thus, if the size of a group is steadily increasing, when it gets to ten it can be broken into two independent groups of five, each of which now fits back into the bottom end of the five to nine range. Applying this rule of seven extension to the reasonable function size example gives a function size range of between 25 and 100 lines.
4
1.3, pgs. 75-97 1.3 Defining C++ Classes Class Definition
The public and private Parts Class Implementation Using Class Clock The Class Person Constructors Modifier and Accessor Member Functions Operators Friends Implementing the Person Class An Application That Uses Class Person Classes as Components of Other Classes Array Data Fields Documentation Style for Classes and Functions 1.3, pgs
5
Classes A class is a data type and a pattern for creating objects.
Software Design A class is a data type and a pattern for creating objects. #ifndef PERSON_H #define PERSON_H #include <string> using std::string; class Person { private: string name; int age; public: Person(string, int); string getName() const; int getAge() const; string toString() const; }; #endif // PERSON_H #include <iostream> #include <string> #include <sstream> #include "person.h" using namespace std; Person::Person(string name, int age) { this->name = name; this->age = age; } string Person::getName() const { return name; } int Person::getAge() const { return age; } string Person::toString() const ostringstream out; out << name << "(" << age << ")"; return out.str(); A class definition describes the names of the class operations and the internal data contents (the data members). instance variables - each class instance (object) has its own storage, usually private to hide implementation details. Member functions (usually public) operate on any object of the class of which it is a member and have access to all the data members of the class object. A class implementation provides the implementations of the operations (the member functions).
6
Class Access Specifiers
Software Design The private access specifier limits access of class members to the class definition. Private data fields Control access to an object's data. Prevent improper use and processing of an object's data. Allow a change of the private data representation without effecting programs that use the class. A friend function of a class has the same access privileges to private and protected data as the class member functions. A friend function is declared by the class that is granting access, so friend functions are part of the class interface, like methods. Since private members of a class are only accessible from within the class, a friend function must also be declared inside of a class, but may be implemented outside of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
7
Friends Software Design #include <iostream> #include <string> using namespace std; class MyClass { private: string name; public: MyClass(const string name) { this->name = name; } ~MyClass() = default; friend std::ostream& operator<< (ostream& os, const MyClass& me) os << "MyClass(" << me.name << ")"; return os; } }; int main(int argc, char* argv[]) MyClass me("Suzy Student"); cout << me << endl; return 0; Each member function has an implicit parameter named this whose value is a pointer to the object with which the member function was called. Defines an independent insertion operator function that is a friend of MyClass. The friend insertion operator function has access to MyClass’ private data members. Returns a reference to the output stream that has been modified.
8
UML Diagrams UML (Unified Modeling Language) classes:
Software Design UML (Unified Modeling Language) classes: class Person { private: string given_name; string family_name; string ID_number; int birth_year; public: int age(); bool can_vote(); bool is_senior(); } Class Person has three string and one int data fields. The Person – string relationship (has-a) is represent by the solid diamonds in the following UML diagram:
9
Unified Modeling Language
Software Design Name Airplane -type:string +Airplane() +setType(type:string):void #getType():string Private Public Protected Is-a Jetplane -engine:JetEngine +Jetplane(type:string,engines:int) +getNumberOfEngines():int JetEngine -number:int +JetEngine(number:int) +getNumber():int Has-a
10
Draw a UML representation of the classes to the left.
class Animal { private: string name; public: Animal() {} void setName(string name) { this->name = name; } string getName() { return name; } }; class License int number; License(int n) : number(n) {} int getNumber() { return number; } class Dog : public Animal License license; Dog(string n, int l) : license(l) { setName(n); } int getLicense() { return license.getNumber(); } int main() Dog* dog = new Dog("Rover", 9999); cout << dog->getName() << " " << dog->getLicense(); delete dog; return 0; }
11
Draw a UML representation of the classes to the left.
class Animal { private: string name; public: Animal() {} void setName(string name) { this->name = name; } string getName() { return name; } }; class License int number; License(int n) : number(n) {} int getNumber() { return number; } class Dog : public Animal License license; Dog(string n, int l) : license(l) { setName(n); } int getLicense() { return license.getNumber(); } int main() Dog* dog = new Dog("Rover", 9999); cout << dog->getName() << " " << dog->getLicense(); delete dog; return 0; } Animal -name:string +Animal() +setName(name:string):void +getName():string Dog -license:License +Dog(name:string,license:int) +getLicense():int License -number:int +License(number:int) +getNumber():int
12
Class/Function Documentation
Software Design Documentation comments precede classes, member functions, and data members in a standard format. Originally was developed for the Java language. DOC++ ( and Doxygen Enclose function descriptions within /** and */. Tags begin with the Use tag for each function parameter. Do not use tag for void functions. /** Encodes a single digit of a POSTNET "A" bar code. @param digit the single digit to encode. @return a bar code of the digit using "|" as the long bar and "," as the half bar. */ string encode(int digit); The first line of the delimitated comment appears in the function summary part of the HTML page.
13
DOC++ Example class Intermediate Inheritance: Public Methods
Software Design /** Just to make the class graph look more interesting. Here we show multiple inheritance from one docified class and a nondocified one. This is how this documentation has been generated: * / class Intermediate : public CommonBase, public NotDocified { … }; class Intermediate Just to make the class graph look more interesting. Inheritance: Inherited from CommonBase: Public Methods const Derived_Class& getB(constIntermediate& c) const Protected Fields double variable
14
1.4 Abstract Data Types, Interfaces, and Pre- and Postconditions
Abstract Data Types (ADTs) and Interfaces An ADT for a Telephone Directory Class Contracts and ADTs Preconditions and Postconditions 1.4, pgs
15
Abstract Data Type (ADT)
Software Design An Abstract Data Type uses encapsulation of data and methods to hide implementation details. Allows for reusable code. The user doesn't know about implementation details. The public part of a class definition defines the interface for the ADT class. Does not implement the class. Does specify the operations performed and/or how the data is represented. ADT's often can be implemented in more than one way. Each class that implements an ADT must provide the complete definition (implementation) of all operations declared in the ADT. Examples: vectors, linked lists, stacks, etc.
16
Pre-conditions and Post-conditions
Software Design A contract between a function and the function programmer. A pre-condition is a statement of any assumptions or constraints on the function's data (input parameters) that are expected to apply before the function is performed. If a pre-condition is not met, there is no guarantee that the function will do what is expected. A post-condition is a statement that describes the results of the function execution. If pre-conditions are met and post-conditions are not, then the function is considered faulty. When implementing an ADT, we document the preconditions and postconditions in the documentation comments. Post-conditions for operations are comments with a void return type that reflect changes to the object's state. If an operation returns a value, you describe the postcondition in the @return tag.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.