fundamentals of programming
play

Fundamentals of Programming Lecture 11 Hamed Rasifard 1 Outline - PowerPoint PPT Presentation

Fundamentals of Programming Lecture 11 Hamed Rasifard 1 Outline Enumerations C++ 2 Enumerations An enumeration is a set of named integer constants. Enumerations are defined much like structures; the keyword enum signals the


  1. Fundamentals of Programming Lecture 11 Hamed Rasifard 1

  2. Outline • Enumerations • C++ 2

  3. Enumerations • An enumeration is a set of named integer constants. • Enumerations are defined much like structures; the keyword enum signals the start of an enumeration type. • The general form for enumerations is: enum tag { enumeration list }; 3

  4. Using Enumeration Type #include <stdio.h> enum coin { penny, nickel, dime, quarter, half_dollar, dollar}; int main( void ) { enum coin money; money = dime; switch(money) { case penny: printf("penny"); break; case nickel: printf("nickel"); break; case dime: printf("dime"); break; case quarter: printf("quarter"); break; case half_dollar: printf(''half_dollar"); break; case dollar: printf("dollar"); } return 0; 4 }

  5. C++ • C++ began as an expanded version of C. • C++ improves on many of C’s features and provides object-oriented-programming (OOP) capabilities that increase software productivity, quality and reusability. This chapter dis- cusses many of C++’s enhancements to C. 5

  6. Object-oriented Programming • a program can be organized in one of two ways: around its code (what is happening) around its data (who is being affected) • Using only structured programming techniques, programs are typically organized around code. • Object-oriented programs organized around data, with the key principle being "data controlling access to code." 6

  7. A Sample C++ Program #include <iostream> using namespace std; int main() { int i; cout << "This is output.\n"; // this is a single line comment /* you can still use C style comments */ // input a number using >> cout << "Enter a number: "; cin >> i; // now, output a number using << cout << i << " squared is " << i*i << "\n"; return 0; } 7

  8. C++ Standard Library • C++ programs consist of pieces called classes and functions. • most C++ programmers take advantage of the rich collections of existing classes and functions in the C++ Standard Library. • You should learn how to use the classes and functions in the C++ Standard Library. 8

  9. Header File • The C++ Standard Library is divided into many portions, each with its own header file. • The header files contain the function prototypes for the related functions that form each portion of the library. • A header file “instructs” the compiler on how to interface with library and user- written components. 9

  10. Namespaces • A namespace is simply a declarative region. • The purpose of a namespace is to localize the names of identifiers to avoid name collisions. • Elements declared in one namespace are separate from elements declared in another. • When you include a new-style header in your program, the contents of that header are contained in the std namespace. 10

  11. C++Classes • A class is a user-defined type. • A class is similar syntactically to a structure. • Classes consist of data members and member functions 11

  12. A class declaration class class-name { private data and functions access-specifier: data and functions access-specifier: data and functions // ... access-specifier: data and functions } object-list; 12

  13. Access Control • There are three kinds of access specifier to data in classes Private Public Protected • Data are private in a class by default 13

  14. A Simple Class #define SIZE 100 // This creates the class stack. class stack { int stck[SIZE]; int tos; public: void init(); void push(int i); int pop(); }; void stack::init() { //Code goes here } void stack::push(int i) { //Code goes here } int stack::pop() { //Code goes here } 14

  15. Scope Resolution Operator • In C++, several different classes can use the same function name. • The :: is called the scope resolution operator. • The compiler knows which function belongs to which class because of the scope resolution operator. 15

  16. Using Classes #include <iostream> using namespace std; #define SIZE 100 // This creates the class stack. class stack { . . . }; . . . int main() { stack stack1, stack2; // create two stack objects stack1.init(); stack2.init(); stack1.push(1); stack2.push(2); stack1.push(3); stack2.push(4); cout << stack1.pop() << " "; cout << stack1.pop() << " "; cout << stack2.pop() << " "; cout << stack2.pop() << "\n"; return 0; 16 }

  17. Constructors • The use of functions such as init() to provide initialization for class objects is inelegant and error- prone. • A better approach is to allow the programmer to declare a function with the explicit purpose of initializing objects. • Because such a function constructs values of a given type, it is called a constructor. • A constructor is recognized by having the same name as the class itself. 17

  18. Constructors(Cont.) • When a class has a constructor, all objects of that class will be initialized. • If the constructor requires arguments, these arguments must be supplied class Date{ int y, m, d; public: //... Date(int, int, int); // day, month, year Date(int, int); // day, month, today’s year Date(int); // day today’s month and year Date(); // default Date: today Date(const char*); // date in string representation }; 18

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend