c++ introduction. why learn c++? very powerful and respected prog. lang. very powerful and respected...

18
C++ C++ Introduction Introduction

Upload: neil-ross

Post on 23-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

C++C++

IntroductionIntroduction

Page 2: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

Why Learn C++?Why Learn C++?

Very powerful and respected prog. Lang.Very powerful and respected prog. Lang. Highly respected in AcademicsHighly respected in Academics B. Tech./M.Tech. Project workB. Tech./M.Tech. Project work Job Written Test + InterviewJob Written Test + Interview Research: Programming work can be done in Research: Programming work can be done in

C++C++

Page 3: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

Only course in C++Only course in C++

This may be the only course in C++ done by This may be the only course in C++ done by you.you.

So learn it wellSo learn it well Benefits, later on in life, can be enormousBenefits, later on in life, can be enormous Excellent Lab facilitiesExcellent Lab facilities Use extra Lab time whenever possibleUse extra Lab time whenever possible But do not neglect other coursesBut do not neglect other courses

Page 4: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

Course BookCourse Book

C++ Primer, C++ Primer, Fourth EditionFourth Edition Authors:Authors:

Stanley B. LippmanStanley B. Lippman Josee LajoieJosee Lajoie Barbara E. MooBarbara E. Moo

Publisher: Pearson EducationPublisher: Pearson Education ISBN: 81-317-1077-7ISBN: 81-317-1077-7 PricePrice

List Price: Rs. 475List Price: Rs. 475 Discount: Usually 10 to 20%Discount: Usually 10 to 20%

Page 5: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

Course Book - 2Course Book - 2

Availability of hard copy enables:Availability of hard copy enables: Easy lookup and studyEasy lookup and study Faster and better learningFaster and better learning

At least one copy for two students is essentialAt least one copy for two students is essential Will be useful after course tooWill be useful after course too Procure book quicklyProcure book quickly Book covers both C and C++Book covers both C and C++ Reading chapters of book will be very usefulReading chapters of book will be very useful

Page 6: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

First C++ ProgramFirst C++ Program

#include <iostream> //Not iostream.h#include <iostream> //Not iostream.h int main()int main() {{

std::cout << “Hello world\n”;std::cout << “Hello world\n”; }}

Page 7: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

First C++ Program – 2First C++ Program – 2

C++ is a superset of CC++ is a superset of C Standard header files do not have .h typicallyStandard header files do not have .h typically The iostream header includes information The iostream header includes information

about stream objects used for input and output.about stream objects used for input and output. All such stream objects are said to be part of All such stream objects are said to be part of

the iostream librarythe iostream library Instead of printf we use std::coutInstead of printf we use std::cout

Page 8: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

First C++ Program - 3First C++ Program - 3

std is a namespacestd is a namespace cout is an object which writes (outputs) to a cout is an object which writes (outputs) to a

stream (the terminal usually)stream (the terminal usually) :: is required between namespace and object:: is required between namespace and object << is the way to direct data into cout object<< is the way to direct data into cout object << is also called output operator<< is also called output operator

Page 9: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

First C++ Program - 4First C++ Program - 4

Source code file extension should be .cppSource code file extension should be .cpp Save file as firstpgm.cppSave file as firstpgm.cpp Compile using c++ or g++Compile using c++ or g++ Executable file is named as a.outExecutable file is named as a.out

Page 10: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

Second C++ ProgramSecond C++ Program

Book code is available as a zip fileBook code is available as a zip file Windows: Windows:

http://www.informit.com/content/images/0201721481/sourcecode/MShttp://www.informit.com/content/images/0201721481/sourcecode/MS_files.zip_files.zip

Linux: Linux: http://www.informit.com/content/images/0201721481/sourcecode/gnuhttp://www.informit.com/content/images/0201721481/sourcecode/gnu_files.tar.gz_files.tar.gz

Copy it to your system (on Linux) and unzip itCopy it to your system (on Linux) and unzip it Chapter 1, add.ccChapter 1, add.cc

.cc is also used as extension for C++ source files but you .cc is also used as extension for C++ source files but you should only use .cpp while you are doing this course.should only use .cpp while you are doing this course.

Page 11: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

Second C++ Program - 2Second C++ Program - 2

<< output operator can be used multiple times << output operator can be used multiple times in one statementin one statement

std::endl is another way of inserting a newline std::endl is another way of inserting a newline (\n)(\n)

Defining int variables is same as CDefining int variables is same as C

Page 12: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

Second C++ Program - 3Second C++ Program - 3

std::cin is an input stream object which usually std::cin is an input stream object which usually reads from keyboardreads from keyboard

>> is the input operator which reads input and >> is the input operator which reads input and puts the input into variables on right hand side puts the input into variables on right hand side of operatorof operator

Page 13: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

Read Course BookRead Course Book

Course book (C++ Primer 4/e) :Course book (C++ Primer 4/e) : Gives a lot of additional information far Gives a lot of additional information far

beyond what we have time to cover in classbeyond what we have time to cover in class Is somewhat precise and so may take getting Is somewhat precise and so may take getting

used toused to

Page 14: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

Read Course Book - 2Read Course Book - 2

But it is very good and very accurateBut it is very good and very accurate Has simple exercisesHas simple exercises Has nice layout with certain paragraphs Has nice layout with certain paragraphs

marked specially as Notes or Beware marked specially as Notes or Beware (warnings)(warnings)

Reading and understanding it thoroughly will Reading and understanding it thoroughly will make you a thorough C++ Gurumake you a thorough C++ Guru

Page 15: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

Hands On (Finally Hands On (Finally ))

Start your systemStart your system Choose Red Hat Linux as OS (instead of Choose Red Hat Linux as OS (instead of

default Windows)default Windows) Log on with userid: - and password: -Log on with userid: - and password: - Wait for the GUI (Graphical User Interface) to Wait for the GUI (Graphical User Interface) to

come up properlycome up properly Open a terminal windowOpen a terminal window

Page 16: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

First AssignmentFirst Assignment

Write the Hello World program using viWrite the Hello World program using vi Source file name must end in .cppSource file name must end in .cpp Compile it using c++ or g++Compile it using c++ or g++ Executable file will be a.out (like what cc Executable file will be a.out (like what cc

does)does) Run ./a.outRun ./a.out

Page 17: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

Second AssignmentSecond Assignment

First copy the book code zip file onto your First copy the book code zip file onto your systemsystem

Unzip the fileUnzip the file Now read 1/add.ccNow read 1/add.cc Then write a program which takes in four Then write a program which takes in four

integral numbers and gives the average of integral numbers and gives the average of them as output.them as output.

Page 18: C++ Introduction. Why Learn C++? Very powerful and respected prog. Lang. Very powerful and respected prog. Lang. Highly respected in Academics Highly

Book Source Code Copyright NoteBook Source Code Copyright Note

The course book is C++ Primer, 4th Edition by The course book is C++ Primer, 4th Edition by Lippman, Lajoie and Moo. Any references in earlier Lippman, Lajoie and Moo. Any references in earlier files to source files, and use of code within those files, files to source files, and use of code within those files, are of example code given in and/or along with the are of example code given in and/or along with the book. As these slides are freely accessible on the book. As these slides are freely accessible on the Internet, not-for-profit, and for educational purposes, Internet, not-for-profit, and for educational purposes, based on the permission related statements in the based on the permission related statements in the source code, I have considered that permission has source code, I have considered that permission has been granted to use them in these slides.been granted to use them in these slides.