Fundamentals of Programming Lecture 19 Hamed Rasifard 1
Outline C++ Object-Oriented Programming Class 2
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. 3
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." 4
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; } 5
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. 6
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 userwritten components. 7
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. 8
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 9
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; 10
Access Control There are three kinds of access specifier to data in classes Private Public Protected Data are private in a class by default 11
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 } 12
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 13
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; } 14
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. 15
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); Date(int, int); Date(int); Date(); Date(const char*); }; // day, month, year // day, month, today s year // day today s month and year // default Date: today // date in string representation 16