<< Chapter < Page Chapter >> Page >

nghĩa là<<liên kết từ trái qua phải. Loại liên kết của các toán tử chèn dòng được phép bởi vì toán tử đa năng hóa<<trả về một tham chiếu tới đối tượng toán hạng bên trái của nó, nghĩa là cout. Vì thế biểu thức đặt trong ngoặc bên cực trái:

(cout<<"47 plus 53 is ")

xuất ra một chuỗi đã chỉ định và trả về một tham chiếu tới cout. Điều này cho phép biểu thức đặt trong ngoặc ở giữa được ước lượng:

(cout<<(47+53))

xuất giá trị nguyên 100 và trả về một tham chiếu tới cout. Sau đó biểu thức đặt trong ngoặc bên cực phải được ước lượng:

cout<<endl;

xuất một newline, flush cout và trả về một tham chiếu tới cout. Trả về cuối cùng này không được sử dụng.

Xuất các biến kiểu char *:

Trong nhập/xuất kiểu C, thật cần thiết cho lập trình viên để cung cấp thông tin kiểu. C++ xác định các kiểu dữ liệu một cách tự động – một cải tiến hay hơn C. Đôi khi điều này là một trở ngại. Chẳng hạn, chúng ta biết rằng một chuỗi ký tự là kiểu char *. Mục đích của chúng ta in giá trị của con trỏ đó, nghĩa là địa chỉ bộ nhớ của ký tự đầu tiên của chuỗi đó. Nhưng toán tử<<đã được đa năng hóa để in dữ liệu của kiểu char * như là chuỗi kết thúc ký tự null. Giải pháp là ép con trỏ thành kiểu void *.

Ví dụ 8.6 : In địa chỉ lưu trong một biến kiểu char *

Chúng ta chạy ví dụ 8.6 , kết quả ở hình 8.8

Hình 8.8: Kết quả của ví dụ 8.6

Xuất ký tự với hàm thành viên put(); nối với nhau hàm put()

Hàm thành viên put() của lớp ostream xuất một ký tự có dạng :

ostream&put(char ch);

Chẳng hạn:

cout.put(‘A’);

Gọi put() có thể được nối vào nhau như:

cout.put(‘A’).put(‘\n’);

Hàm put() cũng có thể gọi với một biểu thức có giá trị là mã ASCII như:

cout.put(65);

Dòng nhập

Dòng nhập có thể được thực hiện với toán tử trích, nghĩa là toán tử đã đa năng hóa>>. Bình thường toán tử này bỏ qua các ký tử khoảng trắng (như các blank, tab và newline). trong dòng nhập. Toán tử trích dòng trả về zero (false) khi kết thúc file (end-of-file) được bắt gặp trên một dòng; Ngược lại, toán tử trích dòng trả về một tham chiếu tới đối tượng xuyên qua đó nó được kéo theo. Mỗi dòng chứa một tập các bit trạng thái (state bit) sử dụng để điều khiển trạng thái của dòng (nghĩa là định dạng, ấn định các trạng thái lỗi,…). Trích dòng sinh ra failbit của dòng được thiết lập nếu dữ liệu của kiểu sai được nhập, và sinh ra badbit của dòng được thiết lập nếu thao tác sai.

Toán tử trích dòng:

Để đọc hai số nguyên sử dụng đối tượng cin và toán tử trích dòng đã đa năng hóa>>.

Ví dụ 8.7:

1: //Chương trình 8.7

2: #include<iostream.h>

3:

4: int main()

5: {

6: int X, Y;

7: cout<<"Enter two integers: ";

8: cin>>X>>Y;

9: cout<<"Sum of "<<X<<" and "<<Y<<" is: "

10:      <<(X + Y)<<endl;

11: return 0;

12: }

Chúng ta chạy ví dụ 8.7 , kết quả ở hình 8.9

Hình 8.9: Kết quả của ví dụ 8.7

Một cách phổ biến để nhập một dãy các giá trị là sử dụng toán tử trích dòng trong vòng lặp while. Toán tử trích dòng trả về false (0) khi end-of-file được bắt gặp:

Ví dụ 8.8:

2: #include<iostream.h>

3:

4: int main()

5: {

6: int Grade, HighestGrade = -1;

7: cout<<"Enter grade (enter end-of-file to end): ";

8: while (cin>>Grade)

Questions & Answers

what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Lập trình hướng đối tượng. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10794/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Lập trình hướng đối tượng' conversation and receive update notifications?

Ask