Procedural Programming vs. OOP • Procedural programming – Writing procedures or functions that perform operations on the data. • Object-oriented programming – Creating objects that contain both data and functions. • Advantages of OOP over procedural programming: – faster and easier to execute – provides a clear structure for the programs – Makes the code easier to maintain, modify and debug – create full reusable applications with less code and shorter development time.
Building blocks of OOP • Classes are user-defined data types that act as the blueprint for individual objects, attributes and methods. • Objects are instances of a class created with specifically defined data. – Objects can correspond to real-world objects or an abstract entity. – When class is defined initially, the description is the only object that is defined. • Methods are functions that are defined inside a class that describe the behaviours of an object. – Each method contained in class definitions starts with a reference to an instance object. – Additionally, the subroutines contained in an object are called instance methods. Programmers use methods for reusability or keeping functionality encapsulated inside one object at a time. • Attributes are defined in the class template and represent the state of an object. Objects will have data stored in the attributes field. Class attributes belong to the class itself.
Principles of OOP/ Features of C++ • Encapsulation. – This principle states that all important information is contained inside an object and only select information is exposed. – The implementation and state of each object are privately held inside a defined class. – Other objects do not have access to this class or the authority to make changes. They are only able to call a list of public functions or methods. – This characteristic of data hiding provides greater program security and avoids unintended data corruption. • Abstraction. – Objects only reveal internal mechanisms that are relevant for the use of other objects, hiding any unnecessary implementation code. – The derived class can have its functionality extended. – This concept can help developers more easily make additional changes or additions over time.
Features of OOP • Inheritance. Classes can reuse code from other classes. – Relationships and subclasses between objects can be assigned, enabling developers to reuse common logic while still maintaining a unique hierarchy. – This property of OOP forces a more thorough data analysis, reduces development time and ensures a higher level of accuracy. • Polymorphism. Objects are designed to share behaviours and they can take on more than one form. – The program will determine which meaning or usage is necessary for each execution of that object from a parent class, reducing the need to duplicate code. – A child class is then created, which extends the functionality of the parent class. – Polymorphism allows different types of objects to pass through the same interface.
Alternative methods to OOP • Functional programming. – This includes languages such as Erlang and Scala, which are used for telecommunications and fault tolerant systems. • Structured or modular programming. – This includes languages such as PHP and C#. • Imperative programming. – This alternative to OOP focuses on function rather than models and includes C++ and Java. • Declarative programming. – This programming method involves statements on what the task or desired outcome is but not how to achieve it. Languages include Prolog and Lisp. • Logical programming. – This method, which is based mostly in formal logic and uses languages such as Prolog, contains a set of sentences that express facts or rules about a problem domain. It focuses on tasks that can benefit from rule-based logical queries. • Note: Most advanced programming languages enable developers to combine models, because they can be used for different programming methods. For example, JavaScript can be used for OOP and functional programming.
Formatted Input and Output Operations
Formatting with flags • The setf() is a member function of the class that is used to set flags for formatting output. syntax -cout.setf(flag, bit-field) • Here, flag defined in the ios class specifies how the output should be formatted bit-field is a constant (defined in ios ) that identifies the group to which the formatting flag belongs to. • There are two types of setf()—one that takes both flag and bit-fields and the other that takes only the flag
Formatting Output Using Manipulators
Data Types in C++ • A type defines a set of values and a set of operations that can be applied on those values. The set of values for each type is known as the domainfor the type. • Data types in C++ is mainly divided into two types: 1. Primitive Data Type (directly by the user to declare variables) ▪ Integer ▪ Character ▪ Boolean ▪ Floating Point ▪ Double Floating Point ▪ Valueless or Void ▪ Wide Character ▪ Also : String 2. Abstract or User Defined Data type (data types are defined by user itself)
Constants • Constants are identifiers whose value does not change. While variables can change their value at any time, constants can never change their value. • Constants are used to define fixed values such as Pi or the charge on an electron so that their value does not get changed in the program even by mistake. • A constant is an explicit data value specified by the programmer. • The value of the constant is known to the compiler at the compile time
Declaring Constants • Rule 1 : Constant names are usually written in capital letters to visually distinguish them from other variable names which are normally written in lower case characters. • Rule 2: No blank spaces are permitted in between the # symbol and define keyword. E.g. #define PI 3.14 • Rule 3: Blank space must be used between #define and constant name and between constant name and constant value. • Rule 4: #define is a preprocessor compiler directive and not a statement. Therefore, it does not end with a semi-colon.
• Pointers are symbolic representation of addresses. • They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Syntax data_type *pointer_variable; • Example int *p,sum Assignment • integer type pointer can hold the address of another int variable • To assign the address of variable to pointer-ampersand symbol (&) p=∑ this pointer holds the address of current object • int num; • this->num=num; Void? • When used in the declaration of a pointer, void specifies that the pointer is "universal." If a pointer's type is void* , the pointer can point to any variable that's not declared with the const or volatile keyword Pointers
Type Conversions • A type cast is basically a conversion from one type to another. There are two types of type conversion: • Implicit Type Conversion Also known as ‘automatic type conversion’. – Done by the compiler on its own, without any external trigger from the user. – Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid lose of data. – All the data types of the variables are upgraded to the data type of the variable with largest data type.bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double – It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float).
Type Conversions • // An example of implicit conversion 1. #include <iostream> 2. using namespace std; 3. int main() 4. { 5. int x = 10; // integer x 6. char y = 'a'; // character c 7. 8. // y implicitly converted to int. ASCII 9. // value of 'a' is 97 10. x = x + y; 11. 12. // x is implicitly converted to float 13. float z = x + 1.0; 14. 15. cout << "x = " << x << endl 16. << "y = " << y << endl 17. << "z = " << z << endl; 18. 19. return 0; 20. } Output:x = 107 y = a z = 108
Type Conversions • Explicit Type Conversion : This process is also called type casting and it is user-defined. Here the user can typecast the result to make it of a particular data type. In C++, it can be done by two ways: • Converting by assignment: This is done by explicitly defining the required type in front of the expression in parenthesis. This can be also considered as forceful casting. • Syntax: (type) expression • where type indicates the data type to which the final result is converted. 1. // C++ program to demonstrate 2. // explicit type casting 3. #include <iostream> 4. using namespace std; 5. int main() 6. { 7. double x = 1.2; 8. // Explicit conversion from double to int 9. int sum = (int)x + 1; 10. cout << "Sum = " << sum; 11. return 0; 12. } Output: Sum = 2
Type Conversions • Conversion using Cast operator: A Cast operator is an unary operator which forces one data type to be converted into another data type. C++ supports four types of casting: – Static Cast – Dynamic Cast – Const Cast – Reinterpret Cast • Example: 1. #include <iostream> 2. using namespace std; 3. int main() 4. { 5. float f = 3.5; 6. // using cast operator 7. int b = static_cast<int>(f); 8. cout << b; 9. } Output: 3
Class and Objects • Class : • A user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects. • For example: • car : an object. • Attributes: weight and color • Methods: drive, apply_brake • Attributes: variables, methods: functions • Attributes and methods are often referred to as "class members".
Create a Class • use the class keyword: • Example • Create a class called "MyClass": • class MyClass { // The class public: // Access specifier int room_num; // Attribute (int variable) string dept; // Attribute (string variable) };
Explanation • public: specifies that members (attributes and methods) of the class are accessible from outside the class. • The class consists of an integer variable room_num and a string-type variable dept. • When variables are declared within a class, they are called attributes. • The class definition ends with a semicolon (;)
Create an Object • To create an object of MyClass, specify the class name, followed by the object name. • To access the class attributes, use the object name, dot operator (.) and variable/attribute name. • Example • Create an object called "myObj" and access the attributes: • class MyClass { // The class public: // Access specifier int room_num; // Attribute (int variable) string dept; // Attribute (string variable) }; int main() { MyClass myObj; // Create an object of MyClass // Access attributes and set values myObj.room_num = 602; myObj.dept = ”dsbs"; // Print attribute values cout << myObj.room_num << "n"; cout << myObj.dept; return 0; }
Access Specifiers • three access specifiers: • public - members are accessible from outside the class • private - members cannot be accessed (or viewed) from outside the class • protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.
Example • class MyClass { public: // Public access specifier int x; // Public attribute private: // Private access specifier int y; // Private attribute }; int main() { MyClass myObj; myObj.x = 25; // Allowed (public) myObj.y = 50; // Not allowed (private) return 0; } • If you try to access a private member, an error occurs: error: y is private
Methods • Methods are functions that belongs to the class. • There are two ways to define functions that belongs to a class: • Inside class definition • Outside class definition • Example • define a function inside the class, and name it "myMethod". • Note: access methods by creating an object of the class, using the dot operator (.), and the method name.
Inside the class class MyClass { // The class public: // Access specifier void myMethod() { // Method inside the class cout << "Hello World!"; } }; int main() { MyClass myObj; // Create an object of MyClass myObj.myMethod(); // Call the method return 0; }
Outside the class • To define a function outside the class definition, declare the method inside the class and then define it outside of the class. • Define outside by specifying the name of the class, followed the scope resolution :: operator, followed by the name of the function. Example class MyClass { // The class public: // Access specifier void myMethod(); // Method/function declaration }; // Method/function definition outside the class void MyClass::myMethod() { cout << "Hello World!"; } int main() { MyClass myObj; // Create an object of MyClass myObj.myMethod(); // Call the method return 0; }
Abstraction and Encapsulation • Abstraction: • Displaying only essential information and hiding the details. • Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation. • In C++, an abstraction is implemented by using a class, a template, or a function. Example: • when we are driving a car, we are only concerned about driving the car like start/stop the car, accelerate/ break, etc. • We do not know how on pressing the accelerator the speed is actually increasing, do not know about the inner mechanism of the car or the implementation of the accelerator, brakes, etc in the car.
Types of Abstraction • Data abstraction • This type only shows the required information about the data and hides the unnecessary data. • Control Abstraction • This type only shows the required information about the implementation and hides unnecessary information.
Example 1 • // C++ Program to Demonstrate the • // working of Abstraction 1. #include <iostream> 2. using namespace std; 3. 4. Class AbstractionDemo { 5. private: 6. int a, b; 7. 8. public: 9. // method to set values of 10. // private members 11. void set(int x, int y) 12. { 13. a = x; 14. b = y; 15. } 16. void display() 17. { 18. cout << "a = " << a << endl; 19. cout << "b = " << b << endl; 20. } 21. }; 22. 23. int main() 24. { 25. AbstractionDemo obj; 26. obj.set(10, 20); 27. obj.display(); 28. return 0; 29. } Output: a = 10 b = 20 we are not allowed to access the variables a and b directly. However, one can call the function set() to set the values in a and b and the function display() to display the values of a and b.
Encapsulation • Hide the sensitive data from users. • To achieve this, we declare class variables/attributes as private so that it cannot be accessed from outside the class. • To allow others to read or modify the value of a private member, we provide public get and set methods. • Encapsulation ensures better control of your data, because you (or others) can change one part of the code without affecting other parts. • It provides an increased security of data.
Example #include <iostream> using namespace std; class Employee { private: // Private attribute int salary; public: // Setter void setSalary(int s) { salary = s; } // Getter int getSalary() { return salary; } }; int main() { Employee myObj; myObj.setSalary(50000); cout << myObj.getSalary(); return 0; }
UML Diagrams Introduction • UML is not a programming language, it is rather a visual language. • We use UML diagrams to portray the behaviour and structure of a system. • UML helps software engineers, businessmen and system architects with modelling, design and analysis. • The Object Management Group (OMG) adopted Unified Modelling Language as a standard in 1997. • International Organization for Standardization (ISO) published UML as an approved standard in 2005.
Classification of UML Diagrams • Structural Diagrams • Capture static aspects or structure of a system. • Component Diagrams, Object Diagrams, Class Diagrams and Deployment Diagrams. • Behaviour Diagrams • Capture dynamic aspects or behaviour of the system. • Use Case Diagrams, State Diagrams, Activity Diagrams and Interaction Diagrams.
Use Case Diagram • Use-case diagrams model the high-level functions and scope of a system. • Helps to identify the interactions between the system and its actors. • The use cases and actors in use-case diagrams describe what the system does and how the actors use it. • but not how the system operates internally.
Example • Create a use case diagram to depict the Online Shopping website. • the Web Customer actor makes use of any online shopping website to purchase online. • The top-level uses are as follows: • View Items, Make Purchase, Checkout, Client Register. • The View Items use case is utilized by the customer who searches and view products. • The Client Register use case allows the customer to register itself with the website for availing gift vouchers, coupons, or getting a private sale invitation. • the Checkout is an included use case, which is part of Making Purchase, and it is not available by itself.
Example
Use case: View Items • The View Items is further extended by several use cases such as., • Search Items, Browse Items, View Recommended Items, Add to Shopping Cart, Add to Wish list. • All of these extended use cases provide some functions to customers, which allows them to search for an item. • Both View Recommended Item and Add to Wish List include the Customer Authentication use case, as they necessitate authenticated customers. • simultaneously item can be added to the shopping cart without any user authentication.
Use case: View Items
Use case: Checkout • The Checkout use case involves Payment use case that can be done either by the credit card and external credit payment services or with PayPal.
Class diagram • It depicts the static structure of a system by showing system’s classes, interfaces, methods, attributes, associations, collaboration, and constraints imposed in the relationships. • Helps to identify relationship between different classes or objects.
Purpose of the class diagram • Analysis and design of the static view of an application. • Describe responsibilities of a system. • Base for component and deployment diagrams. • Forward and reverse engineering.
Example

OOP UNIT 1_removed ppt explaining object.pdf

  • 1.
    Procedural Programming vs.OOP • Procedural programming – Writing procedures or functions that perform operations on the data. • Object-oriented programming – Creating objects that contain both data and functions. • Advantages of OOP over procedural programming: – faster and easier to execute – provides a clear structure for the programs – Makes the code easier to maintain, modify and debug – create full reusable applications with less code and shorter development time.
  • 2.
    Building blocks ofOOP • Classes are user-defined data types that act as the blueprint for individual objects, attributes and methods. • Objects are instances of a class created with specifically defined data. – Objects can correspond to real-world objects or an abstract entity. – When class is defined initially, the description is the only object that is defined. • Methods are functions that are defined inside a class that describe the behaviours of an object. – Each method contained in class definitions starts with a reference to an instance object. – Additionally, the subroutines contained in an object are called instance methods. Programmers use methods for reusability or keeping functionality encapsulated inside one object at a time. • Attributes are defined in the class template and represent the state of an object. Objects will have data stored in the attributes field. Class attributes belong to the class itself.
  • 3.
    Principles of OOP/Features of C++ • Encapsulation. – This principle states that all important information is contained inside an object and only select information is exposed. – The implementation and state of each object are privately held inside a defined class. – Other objects do not have access to this class or the authority to make changes. They are only able to call a list of public functions or methods. – This characteristic of data hiding provides greater program security and avoids unintended data corruption. • Abstraction. – Objects only reveal internal mechanisms that are relevant for the use of other objects, hiding any unnecessary implementation code. – The derived class can have its functionality extended. – This concept can help developers more easily make additional changes or additions over time.
  • 4.
    Features of OOP •Inheritance. Classes can reuse code from other classes. – Relationships and subclasses between objects can be assigned, enabling developers to reuse common logic while still maintaining a unique hierarchy. – This property of OOP forces a more thorough data analysis, reduces development time and ensures a higher level of accuracy. • Polymorphism. Objects are designed to share behaviours and they can take on more than one form. – The program will determine which meaning or usage is necessary for each execution of that object from a parent class, reducing the need to duplicate code. – A child class is then created, which extends the functionality of the parent class. – Polymorphism allows different types of objects to pass through the same interface.
  • 5.
    Alternative methods toOOP • Functional programming. – This includes languages such as Erlang and Scala, which are used for telecommunications and fault tolerant systems. • Structured or modular programming. – This includes languages such as PHP and C#. • Imperative programming. – This alternative to OOP focuses on function rather than models and includes C++ and Java. • Declarative programming. – This programming method involves statements on what the task or desired outcome is but not how to achieve it. Languages include Prolog and Lisp. • Logical programming. – This method, which is based mostly in formal logic and uses languages such as Prolog, contains a set of sentences that express facts or rules about a problem domain. It focuses on tasks that can benefit from rule-based logical queries. • Note: Most advanced programming languages enable developers to combine models, because they can be used for different programming methods. For example, JavaScript can be used for OOP and functional programming.
  • 6.
  • 7.
    Formatting with flags •The setf() is a member function of the class that is used to set flags for formatting output. syntax -cout.setf(flag, bit-field) • Here, flag defined in the ios class specifies how the output should be formatted bit-field is a constant (defined in ios ) that identifies the group to which the formatting flag belongs to. • There are two types of setf()—one that takes both flag and bit-fields and the other that takes only the flag
  • 9.
  • 10.
    Data Types inC++ • A type defines a set of values and a set of operations that can be applied on those values. The set of values for each type is known as the domainfor the type. • Data types in C++ is mainly divided into two types: 1. Primitive Data Type (directly by the user to declare variables) ▪ Integer ▪ Character ▪ Boolean ▪ Floating Point ▪ Double Floating Point ▪ Valueless or Void ▪ Wide Character ▪ Also : String 2. Abstract or User Defined Data type (data types are defined by user itself)
  • 12.
    Constants • Constants areidentifiers whose value does not change. While variables can change their value at any time, constants can never change their value. • Constants are used to define fixed values such as Pi or the charge on an electron so that their value does not get changed in the program even by mistake. • A constant is an explicit data value specified by the programmer. • The value of the constant is known to the compiler at the compile time
  • 13.
    Declaring Constants • Rule1 : Constant names are usually written in capital letters to visually distinguish them from other variable names which are normally written in lower case characters. • Rule 2: No blank spaces are permitted in between the # symbol and define keyword. E.g. #define PI 3.14 • Rule 3: Blank space must be used between #define and constant name and between constant name and constant value. • Rule 4: #define is a preprocessor compiler directive and not a statement. Therefore, it does not end with a semi-colon.
  • 18.
    • Pointers aresymbolic representation of addresses. • They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Syntax data_type *pointer_variable; • Example int *p,sum Assignment • integer type pointer can hold the address of another int variable • To assign the address of variable to pointer-ampersand symbol (&) p=&sum; this pointer holds the address of current object • int num; • this->num=num; Void? • When used in the declaration of a pointer, void specifies that the pointer is "universal." If a pointer's type is void* , the pointer can point to any variable that's not declared with the const or volatile keyword Pointers
  • 20.
    Type Conversions • Atype cast is basically a conversion from one type to another. There are two types of type conversion: • Implicit Type Conversion Also known as ‘automatic type conversion’. – Done by the compiler on its own, without any external trigger from the user. – Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid lose of data. – All the data types of the variables are upgraded to the data type of the variable with largest data type.bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double – It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float).
  • 21.
    Type Conversions • //An example of implicit conversion 1. #include <iostream> 2. using namespace std; 3. int main() 4. { 5. int x = 10; // integer x 6. char y = 'a'; // character c 7. 8. // y implicitly converted to int. ASCII 9. // value of 'a' is 97 10. x = x + y; 11. 12. // x is implicitly converted to float 13. float z = x + 1.0; 14. 15. cout << "x = " << x << endl 16. << "y = " << y << endl 17. << "z = " << z << endl; 18. 19. return 0; 20. } Output:x = 107 y = a z = 108
  • 22.
    Type Conversions • ExplicitType Conversion : This process is also called type casting and it is user-defined. Here the user can typecast the result to make it of a particular data type. In C++, it can be done by two ways: • Converting by assignment: This is done by explicitly defining the required type in front of the expression in parenthesis. This can be also considered as forceful casting. • Syntax: (type) expression • where type indicates the data type to which the final result is converted. 1. // C++ program to demonstrate 2. // explicit type casting 3. #include <iostream> 4. using namespace std; 5. int main() 6. { 7. double x = 1.2; 8. // Explicit conversion from double to int 9. int sum = (int)x + 1; 10. cout << "Sum = " << sum; 11. return 0; 12. } Output: Sum = 2
  • 23.
    Type Conversions • Conversionusing Cast operator: A Cast operator is an unary operator which forces one data type to be converted into another data type. C++ supports four types of casting: – Static Cast – Dynamic Cast – Const Cast – Reinterpret Cast • Example: 1. #include <iostream> 2. using namespace std; 3. int main() 4. { 5. float f = 3.5; 6. // using cast operator 7. int b = static_cast<int>(f); 8. cout << b; 9. } Output: 3
  • 24.
    Class and Objects •Class : • A user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects. • For example: • car : an object. • Attributes: weight and color • Methods: drive, apply_brake • Attributes: variables, methods: functions • Attributes and methods are often referred to as "class members".
  • 25.
    Create a Class •use the class keyword: • Example • Create a class called "MyClass": • class MyClass { // The class public: // Access specifier int room_num; // Attribute (int variable) string dept; // Attribute (string variable) };
  • 26.
    Explanation • public: specifiesthat members (attributes and methods) of the class are accessible from outside the class. • The class consists of an integer variable room_num and a string-type variable dept. • When variables are declared within a class, they are called attributes. • The class definition ends with a semicolon (;)
  • 27.
    Create an Object •To create an object of MyClass, specify the class name, followed by the object name. • To access the class attributes, use the object name, dot operator (.) and variable/attribute name. • Example • Create an object called "myObj" and access the attributes: • class MyClass { // The class public: // Access specifier int room_num; // Attribute (int variable) string dept; // Attribute (string variable) }; int main() { MyClass myObj; // Create an object of MyClass // Access attributes and set values myObj.room_num = 602; myObj.dept = ”dsbs"; // Print attribute values cout << myObj.room_num << "n"; cout << myObj.dept; return 0; }
  • 28.
    Access Specifiers • threeaccess specifiers: • public - members are accessible from outside the class • private - members cannot be accessed (or viewed) from outside the class • protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.
  • 29.
    Example • class MyClass{ public: // Public access specifier int x; // Public attribute private: // Private access specifier int y; // Private attribute }; int main() { MyClass myObj; myObj.x = 25; // Allowed (public) myObj.y = 50; // Not allowed (private) return 0; } • If you try to access a private member, an error occurs: error: y is private
  • 30.
    Methods • Methods arefunctions that belongs to the class. • There are two ways to define functions that belongs to a class: • Inside class definition • Outside class definition • Example • define a function inside the class, and name it "myMethod". • Note: access methods by creating an object of the class, using the dot operator (.), and the method name.
  • 31.
    Inside the class classMyClass { // The class public: // Access specifier void myMethod() { // Method inside the class cout << "Hello World!"; } }; int main() { MyClass myObj; // Create an object of MyClass myObj.myMethod(); // Call the method return 0; }
  • 32.
    Outside the class •To define a function outside the class definition, declare the method inside the class and then define it outside of the class. • Define outside by specifying the name of the class, followed the scope resolution :: operator, followed by the name of the function. Example class MyClass { // The class public: // Access specifier void myMethod(); // Method/function declaration }; // Method/function definition outside the class void MyClass::myMethod() { cout << "Hello World!"; } int main() { MyClass myObj; // Create an object of MyClass myObj.myMethod(); // Call the method return 0; }
  • 33.
    Abstraction and Encapsulation •Abstraction: • Displaying only essential information and hiding the details. • Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation. • In C++, an abstraction is implemented by using a class, a template, or a function. Example: • when we are driving a car, we are only concerned about driving the car like start/stop the car, accelerate/ break, etc. • We do not know how on pressing the accelerator the speed is actually increasing, do not know about the inner mechanism of the car or the implementation of the accelerator, brakes, etc in the car.
  • 34.
    Types of Abstraction •Data abstraction • This type only shows the required information about the data and hides the unnecessary data. • Control Abstraction • This type only shows the required information about the implementation and hides unnecessary information.
  • 35.
    Example 1 • //C++ Program to Demonstrate the • // working of Abstraction 1. #include <iostream> 2. using namespace std; 3. 4. Class AbstractionDemo { 5. private: 6. int a, b; 7. 8. public: 9. // method to set values of 10. // private members 11. void set(int x, int y) 12. { 13. a = x; 14. b = y; 15. } 16. void display() 17. { 18. cout << "a = " << a << endl; 19. cout << "b = " << b << endl; 20. } 21. }; 22. 23. int main() 24. { 25. AbstractionDemo obj; 26. obj.set(10, 20); 27. obj.display(); 28. return 0; 29. } Output: a = 10 b = 20 we are not allowed to access the variables a and b directly. However, one can call the function set() to set the values in a and b and the function display() to display the values of a and b.
  • 36.
    Encapsulation • Hide thesensitive data from users. • To achieve this, we declare class variables/attributes as private so that it cannot be accessed from outside the class. • To allow others to read or modify the value of a private member, we provide public get and set methods. • Encapsulation ensures better control of your data, because you (or others) can change one part of the code without affecting other parts. • It provides an increased security of data.
  • 37.
    Example #include <iostream> using namespacestd; class Employee { private: // Private attribute int salary; public: // Setter void setSalary(int s) { salary = s; } // Getter int getSalary() { return salary; } }; int main() { Employee myObj; myObj.setSalary(50000); cout << myObj.getSalary(); return 0; }
  • 38.
    UML Diagrams Introduction •UML is not a programming language, it is rather a visual language. • We use UML diagrams to portray the behaviour and structure of a system. • UML helps software engineers, businessmen and system architects with modelling, design and analysis. • The Object Management Group (OMG) adopted Unified Modelling Language as a standard in 1997. • International Organization for Standardization (ISO) published UML as an approved standard in 2005.
  • 39.
    Classification of UMLDiagrams • Structural Diagrams • Capture static aspects or structure of a system. • Component Diagrams, Object Diagrams, Class Diagrams and Deployment Diagrams. • Behaviour Diagrams • Capture dynamic aspects or behaviour of the system. • Use Case Diagrams, State Diagrams, Activity Diagrams and Interaction Diagrams.
  • 40.
    Use Case Diagram •Use-case diagrams model the high-level functions and scope of a system. • Helps to identify the interactions between the system and its actors. • The use cases and actors in use-case diagrams describe what the system does and how the actors use it. • but not how the system operates internally.
  • 41.
    Example • Create ause case diagram to depict the Online Shopping website. • the Web Customer actor makes use of any online shopping website to purchase online. • The top-level uses are as follows: • View Items, Make Purchase, Checkout, Client Register. • The View Items use case is utilized by the customer who searches and view products. • The Client Register use case allows the customer to register itself with the website for availing gift vouchers, coupons, or getting a private sale invitation. • the Checkout is an included use case, which is part of Making Purchase, and it is not available by itself.
  • 42.
  • 43.
    Use case: ViewItems • The View Items is further extended by several use cases such as., • Search Items, Browse Items, View Recommended Items, Add to Shopping Cart, Add to Wish list. • All of these extended use cases provide some functions to customers, which allows them to search for an item. • Both View Recommended Item and Add to Wish List include the Customer Authentication use case, as they necessitate authenticated customers. • simultaneously item can be added to the shopping cart without any user authentication.
  • 44.
  • 45.
    Use case: Checkout •The Checkout use case involves Payment use case that can be done either by the credit card and external credit payment services or with PayPal.
  • 46.
    Class diagram • Itdepicts the static structure of a system by showing system’s classes, interfaces, methods, attributes, associations, collaboration, and constraints imposed in the relationships. • Helps to identify relationship between different classes or objects.
  • 47.
    Purpose of theclass diagram • Analysis and design of the static view of an application. • Describe responsibilities of a system. • Base for component and deployment diagrams. • Forward and reverse engineering.
  • 48.