fundamental of programming (c) group...

19
Lecturer: Vahid Khodabakhshi Sharif University of Technology Department of Computer Engineering 1/19 Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Group 4 Lecture 12 Introduction to C++, Object Oriented Programming CE 40153 - Fall 98

Upload: others

Post on 24-Feb-2020

26 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Lecturer: Vahid Khodabakhshi

Sharif University of TechnologyDepartment of Computer Engineering 1/19

Bo

rro

wed

fro

m le

ctu

rer

no

tes

by

Om

id J

afar

inez

had

Fundamental of Programming (C) Group 4

Lecture 12

Introduction to C++, Object Oriented Programming

CE 40153 - Fall 98

Page 2: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 2/19

C++ Overview• C++ was designed at AT&T Bell Labs

– by Bjarne Stroustrup in the early 80’s

– nearly 30 years ago!

• C++ is compatible extension of C that provides:– Stronger type-checking

– Support for data abstraction

– Support for object-oriented programming

– Support for generic programming

2

Page 3: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 3/19

C++ Design Goals• As with C, run-time efficiency is important

– Unlike other languages (e.g., Ada, Java, C#, etc.) complicated run-time libraries and virtual machines have not traditionally been required for C++• Note, that there is no language-specific support for

concurrency, persistence, or distribution in C++

• Compatibility with C libraries & traditionaldevelopment tools

• As close to C as possible, but no closer

3

Page 4: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 4/19

C++ Enhancements• C++ supports data abstraction & encapsulation

– e.g., the class mechanism & name spaces

• C++ supports object-oriented programming features– e.g., abstract classes, inheritance, & virtual methods

• C++ supports generic programming– e.g., parameterized types

• C++ supports sophisticated error handling– e.g., exception handling

• C++ supports identifying an object’s type at runtime– e.g., Run-Time Type Identification (RTTI)

4

Page 5: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 5/19

Language Features Not Part of C++

• Concurrency

• Persistence

• Garbage Collection

• Distribution

5

Page 6: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 6/19

Extensions to C• Function Overloading

6

#include <stdio.h> void show(int val) { printf("Integer: %d\n", val); }void show(double val) { printf("Double: %lf\n", val); } void show(char const *val) { printf("String: %s\n", val); }

int main() {

show(12); show(3.1415); show("Hello World!\n");

}

Page 7: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 7/19

Extensions to C• Function Overloading:

– Do not use function overloading for functionsdoing conceptually different tasks.

– C++ does not allow identically named functions to differ only in their return values.

7

Page 8: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 8/19

Extensions to C• Default function arguments

8

#include <stdio.h>

int Sum(int a = 1, int b = 4) {return a + b;}

int main(){

printf("%d", Sum()); // arguments: 1 + 4printf("%d”, Sum(20)); // arguments: 20 + 4printf("%d", Sum(20, 5)); // arguments: 20 + 5// Sum(,6); // Error

}

Page 9: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 9/19

Extensions to C• Default function arguments

– Default arguments must be known at compile-time since at that moment arguments aresupplied to functions. Therefore, the defaultarguments must be mentioned at the function'sdeclaration, rather than at its implementation:

9

// sample header file extern void two_ints(int a = 1, int b = 4);

// code of function in, filename.ccpvoid two_ints(int a, int b) { ... }

Page 10: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 10/19

Extensions to C

10

#include <iostream>using namespace std;int main(){

int ival;char sval[30];std::cout << "Enter a number:\n"; // <<, insertion operator cin >> ival; // >>, extraction operatorcout << "And now a string:\n";cin >> sval;

cout << "The number is: " << ival << "\n""And the string is: " << sval << '\n';

}

Page 11: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 11/19

Extensions to C• Bool

11

bool bValue; // true (!=0) or false (0)

bool bValue1 = true; // explicit assignmentbool bValue2(false); // implicit assignmentbool bValue1 = !true; // bValue1 will have the value falsebool bValue2(!false); // bValue2 will have the value true

bool bValue = true; // bool bValue = 30; cout << bValue << endl; // 1cout << !bValue << std::endl; // 0

if (!bValue)cout << "The if statement was true" << endl;

elsecout << "The if statement was false" << endl;

Page 12: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 12/19

IOStreams• File

– ifstream (derived from istream)

• file input

– ofstream (derived from ostream)

• output file

– fstream (derived from iostream).

• input/output file

– fstream.h

12

Page 13: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 13/19

IOStreams

• File output

13

#include <fstream>#include <iostream> // ofstream is used for writing files. // We'll make a file called Sample.datofstream outf("Sample.dat");

// If we couldn't open the output file stream for writingif (!outf) // Print an error and exit{ … }

// We'll write two lines into this fileoutf << "This is line 1" << endl;outf << "This is line 2" << endl;

Page 14: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 14/19

IOStreams

• Output File– ifstream returns a 0 if we’ve reached the end of the file (EOF)

14

#include <fstream>#include <iostream>

// ifstream is used for reading files// We'll read from a file called Sample.dat

ifstream inf("Sample.dat");// If we couldn't open the output file stream for writingif (!inf) // Print an error and exit{ … }// While there's still stuff left to readwhile (inf){

// read stuff from the file into a string and print itstd::string strInput;

inf >> strInput; getline(inf, strInput);cout << strInput << endl;

}

Page 15: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 15/19

Classes and class members

15

struct DateStruct{

int nMonth;int nDay;int nYear;

};// Here is a function to initialize a datevoid SetDate(DateStruct &sDate, int nMonth, int nDay, int nYear){

sDate.nMonth = nMonth;sDate.nDay = nDay;sDate.nYear = nYear;

}// … In main …DateStruct sToday;// Initialize it manuallysToday.nMonth = 10;sToday.nDay = 14;sToday.nYear = 2040;SetDate(sToday, 10, 14, 2020);

Page 16: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 16/19

Classes and class members

16

struct DateStruct{

int nMonth;int nDay;int nYear;

};

class Date{public:

int m_nMonth;int m_nDay;int m_nYear;

};Date cToday; // declare a Object of class Date

// Assign values to our members using the member selector operator (.)cToday.m_nMonth = 10;cToday.m_nDay = 14;cToday.m_nYear = 2020;

Page 17: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 17/19

Access functions and encapsulation

17

class Date{

private:int m_nMonth;int m_nDay;int m_nYear;

public:// Gettersint GetMonth() { return m_nMonth; }int GetDay() { return m_nDay; }int GetYear() { return m_nYear; }

// Settersvoid SetMonth(int nMonth) { m_nMonth = nMonth; }void SetDay(int nDay) { m_nDay = nDay; }void SetYear(int nYear) { m_nYear = nYear; }

};

Page 18: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 18/19

Public vs. private access specifiers

18

class Change{

public: int m_nValue;

};

int main(){

Change cChange;cChange.m_nValue = 5;std::cout << cChange.m_nValue

<< std::endl;};

class Change{private:

int m_nValue;

public:void SetValue(int nValue) { m_nValue = nValue; }int GetValue() { return m_nValue; }

};int main()

{Change cChange;cChange.SetValue(5);std::cout << cChange.GetValue()

<< std::endl;}

Page 19: Fundamental of Programming (C) Group 4ce.sharif.edu/courses/98-99/1/ce153-4/resources/root/Lectures/C40153-Fall98-Lecture12.pdfIntroduction to C++, Object Oriented Programming –Lecture

Introduction to C++, Object Oriented Programming – Lecture 12

Sharif University of TechnologyDepartment of Computer Engineering 19/19

Constructors

• A constructor is a special kind of class member function that isexecuted when an object of that class is instantiated

• Constructors are typically used to initialize member variables ofthe class to appropriate default values, or to allow the user toeasily initialize those member variables to whatever values aredesired

• Constructors have specific rules for how they must be named:– Constructors should always have the same name as the class – Constructors have no return type (not even void)

• A constructor that takes no parameters (or has all optionalparameters) is called a default constructor

19