1 Using Object
1 Using Object
Object-Oriented Programming
Using C++
Course Contents
Introduction to object-oriented programming...
...with a strong software engineering foundation...
...aimed at producing and maintaining large, high-quality software systems.
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
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
Introduction to C++
The trip begins...
学哪个语言
https://www.tiobe.com/tiobe-index/
其他语言?
几乎都是C-like语言
现代的编程语言在语法上的差异很小
语言的能力/适用领域主要是由
库, and
传统所决定的
结论建议
对计算机本身(体系结构、操作系统、编译)感兴 趣—>C
想编程解决手头的问题(统计、AI、桌面小程序) —>Python
有明确的需求(求职)—>人家要什么学什么 (PHP、JavaScript、C++)
C++是写库的语言
还没想好 —>Java
Introduction to C++
The trip begins...
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
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
int main()
{
cout << "Hello, World! I am " << 18 < Today!" << endl;
return 0;
}
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;
}
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)
string
string is a class in C++
You must add this at the beginning of your code:
#include <string>
Object
A string variable is an object.
Everything is an object.
by Alan Kay
A variable of any type in C++ is an object.
length
s.length();
Create a string
string(const char *cp, int len);
string(const string& s2, int pos);
string(const string& s2, int pos, int len);
Sub-string
substr(int pos, int len);
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);
Search string
size_t find (const string& str, size_t pos = 0) const;
Pointers to Objects
string s = "hello";
string* ps = &s;
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
Assignment
string s1, s2;
s1 = s2;
string *ps1, *ps2;
ps1 = ps2;