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

fundamentals of programming
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Fundamentals of Programming

Lecture 11 Hamed Rasifard

1

slide-2
SLIDE 2

2

Outline

  • Enumerations
  • C++
slide-3
SLIDE 3

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 };

slide-4
SLIDE 4

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; }

slide-5
SLIDE 5

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.

slide-6
SLIDE 6

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."

slide-7
SLIDE 7

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; }

slide-8
SLIDE 8

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.

slide-9
SLIDE 9

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.

slide-10
SLIDE 10

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.

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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;

slide-13
SLIDE 13

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
slide-14
SLIDE 14

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 }

slide-15
SLIDE 15

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.

slide-16
SLIDE 16

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; }

slide-17
SLIDE 17

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.

slide-18
SLIDE 18

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 };