0% found this document useful (0 votes)
6 views

1 Using Object

Uploaded by

ZZZUQ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

1 Using Object

Uploaded by

ZZZUQ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

OOP W1: Using Object

Object-Oriented Programming
Using C++

2002-2024 Weng Kai


OOP W1: Using Object

Course Contents
Introduction to object-oriented programming...
...with a strong software engineering foundation...
...aimed at producing and maintaining large, high-quality software systems.

2002-2024 Weng Kai 2


OOP W1: Using Object
Buzzwords
encapsulation
inheritance
polymorphism
overriding
interface
cohesion
coupling
collection classes
template
responsibility-driven design
2002-2024 Weng Kai 3
OOP W1: Using Object

Textbooks
C++ Prime, Ver 5
Thinking In C++, Ver. 2,Vol. 1 & 2
References:
The C++ Programming Language
C++: The Core Language
Essential C++
Effective C++
Inside the C++ Object Model
C++ Templates
2002-2024 Weng Kai 4
OOP W1: Using Object

Bruce Eckel
Bruce Eckel is the author of “Thinking in C++” , who won the Software Development
Jolt Award for best book of 1995. He's been professionally programming for 20 years
and has been teaching people throughout the world how to program with objects
since 1986. He was a voting member of the C++ Standards Committee.
http://mindview.net

2002-2024 Weng Kai 5


OOP W1: Using Object

Assessment
0. Attendance: 1%
1. In-class quiz: 8%, mostly before lecture, on PTA, since W3
2. Assignments: 16%, one problem set for each week, on PTA, due next lecture (W1 and
W2 due W4 lecture)
3. 6 Labs: 12%, due Sat (W1 due W4 sat.)
4. 2 Projects: 8%
5. Mid-Term Exam: 5% on PTA, 90-min at lab period, around week 9
6. Final Exam: 50%
2002-2024 Weng Kai 6
OOP W1: Using Object

Tools for C++


TDM GCC64 (Windows) or
clang+llvm (macOS)
Visual Studio Code
学在浙大有安装视频
可以继续使用Dev C++

2002-2024 Weng Kai 7


OOP W1: Using Object

Introduction to C++
The trip begins...

2002-2024 Weng Kai 8


OOP W1: Using Object

学哪个语言
https://www.tiobe.com/tiobe-index/

2002-2024 Weng Kai 9


OOP W1: Using Object

其他语言?
几乎都是C-like语言
现代的编程语言在语法上的差异很小
语言的能力/适用领域主要是由
库, and
传统所决定的

2002-2024 Weng Kai 10


OOP W1: Using Object

结论建议
对计算机本身(体系结构、操作系统、编译)感兴 趣—>C
想编程解决手头的问题(统计、AI、桌面小程序) —>Python
有明确的需求(求职)—>人家要什么学什么 (PHP、JavaScript、C++)
C++是写库的语言
还没想好 —>Java

2002-2024 Weng Kai 11


OOP W1: Using Object

Introduction to C++
The trip begins...

2002-2024 Weng Kai 12


OOP W1: Using Object

The C Language
Strengths
Efficient programs
Direct access to machine, suitable for OS and ES
Flexible
Weakness
Insufficient type checking
Poor support for programming-in-the-large
Procedure-oriented programming
2002-2024 Weng Kai 13
OOP W1: Using Object

Bjarne Stroustrup
http://www.research.att.com/~bs/homepage.html
C++ was first designed and implemented by Bjarne Stroustrup, AT&T, early 1980’s
Oct. 2002, Stroustrup visited Zhejiang Univ.
The Design and Evolution of C++, Bjarne Stroustrup, Addison-Wesley, ISBN 0-201-
54330-3

2002-2024 Weng Kai 14


OOP W1: Using Object

Brief history of C++ (1)


1978: BS at Cambridge, UK.
Simulation program in Simula
Supports classes, inheritance, and type check Poor performance

2002-2024 Weng Kai 15


OOP W1: Using Object

Brief history of C++ (2)


1979: BS at AT&T Labs, Cpre, C w/ classes
1980: most C++ features but virtual functions
1983: C++ w/ virtual functions, named C++ by Rick Mascitti 1985: Cfront
1985:“The C++ Programming Language”
1990:ANSI C++ Committee ISO/ANSI Standard C++ in 1998: ISO/IEC 14882
(http://www.open-std.org/jtc1/sc22/ wg21/ )

2002-2024 Weng Kai 16


OOP W1: Using Object

Goal for C++


Support for object oriented programming (from SmallTalk)
to combine flexibility and efficiency of C

2002-2024 Weng Kai 17


OOP W1: Using Object

C and C++
C++ builds on C
Knowledge of C helps you in C++
C++ support more styles of programming
C++ provides more features 26

2002-2024 Weng Kai 18


OOP W1: Using Object
C++ improvements
Data abstraction
Access control
Initialization & cleanup
References
Function overloading
Streams for I/O
Name control
Operator overloading
More safe and powerful memory management
Templates
Exception handling
2002-2024 Weng Kai 19
OOP W1: Using Object

C++ C++ can be viewed as a “better” C


C++ => C=C+1
but...
C++ is not C
Focus on C++ as a language in its own right
C++ is a hybrid language, supports
Procedure-oriented programming
Object-oriented programming
Generic programming

2002-2024 Weng Kai 20


OOP W1: Using Object

The First C++ Program


#include <iostream>
using namespace std;

int main()
{
cout << "Hello, World! I am " << 18 < Today!" << endl;
return 0;
}

2002-2024 Weng Kai 21


OOP W1: Using Object

Read input
#include <iostream>
using namespace std;

int main() {
int number;
cout << "Enter a decimal number: ";
cin >> number;
cout << "The number you entered is " << number <<" endl;
return 0;
}

2002-2024 Weng Kai 22


OOP W1: Using Object

Format output
Manipulators are special functions that can be included in the I/O statement to alter
the format parameters of a stream.
endl is one of the manipulators.

#include <iomanip>

setw(int width)

setprecision(int place)

cout << setw(10) << setprecision(2) << 12.856 << endl;

2002-2024 Weng Kai 23


OOP W1: Using Object

string
string is a class in C++
You must add this at the beginning of your code:
#include <string>

Define variable of string like other types:


string str;

Initialize it w/ string literal:


string str = "Hello";
string str("Hello");
string str{"Hello"};

2002-2024 Weng Kai 24


OOP W1: Using Object

Object
A string variable is an object.
Everything is an object.
by Alan Kay
A variable of any type in C++ is an object.

2002-2024 Weng Kai 25


OOP W1: Using Object

Assignment for string


char charr1[20];
char charr2[20] = "jaguar";
string str1;
string str2 = "panther";
carr1 = char2; // illegal
str1 = str2; // legal

2002-2024 Weng Kai 26


OOP W1: Using Object

Input and Output


Read and write string with cin / cout :
cin >> str;
cout << str;

Read a whole line:


getline(cin, line_var)

2002-2024 Weng Kai 27


OOP W1: Using Object

One character in a string


A string can be used as an array:
string s = "Hello";
s[0]='J';

2002-2024 Weng Kai 28


OOP W1: Using Object

Concatenation for string


string str3;
str3 = str1 + str2;
str1 += str2;
str1 += "lalala";

2002-2024 Weng Kai 29


OOP W1: Using Object

length
s.length();

The dot . is an operator that retrieve a member of a struct in C


It is still that operator in C++ that retrieve a member of an object in C

2002-2024 Weng Kai 30


OOP W1: Using Object

Create a string
string(const char *cp, int len);
string(const string& s2, int pos);
string(const string& s2, int pos, int len);

2002-2024 Weng Kai 31


OOP W1: Using Object

Sub-string
substr(int pos, int len);

2002-2024 Weng Kai 32


OOP W1: Using Object

Alter string
insert(size_t pos, const string& s);
erase (size_t pos = 0, size_t len = npos);
append (const string& str);
replace (size_t pos, size_t len, const string& str);

2002-2024 Weng Kai 33


OOP W1: Using Object

Search string
size_t find (const string& str, size_t pos = 0) const;

2002-2024 Weng Kai 34


OOP W1: Using Object

Pointers to Objects
string s = "hello";
string* ps = &s;

2002-2024 Weng Kai 35


OOP W1: Using Object

Operators with Pointers


& : get address
ps = &s;

* : get the object


(*ps).length()

-> : call the function


ps->length()

2002-2024 Weng Kai 36


OOP W1: Using Object

Two Ways to Access


string s;
s is the object itself
string *ps;
ps is a pointer to an object

2002-2024 Weng Kai 37


OOP W1: Using Object

Object vs Pointer
string s;
At this line, object s is created and initialized
string *ps;
At this line, the object ps points to is not known yet

2002-2024 Weng Kai 38


OOP W1: Using Object

Assignment
string s1, s2;
s1 = s2;
string *ps1, *ps2;
ps1 = ps2;

2002-2024 Weng Kai 39


OOP W1: Using Object

What we’ve learned today?


A brief history of C++
Input and output in C++ with cin and cout (Ch1.2)
The string class, the dot . operator (Ch3.2)
Pointer to an object, the arrow -> operator

2002-2024 Weng Kai 40

You might also like