Data Structures and Programming with C++ By Dr. Atul Dwivedi ETC, BIT, Durg UNIT-I B. E., V Semester
Outline Objectives of the Course Syllabus Reference Books Introduction to programming languages Basic C++ Programs Quiz 2
Objectives of the course To gain a better understanding of object-oriented design and program implementation by using object oriented features of C++. Identify the major elements in an object-oriented programming language. Understand some advanced features of C++ including templates, exceptions, and multiple inheritances. 3
Syllabus 4
Reference Books 5
Introduction to programming languages Program Machine language Assembly language High level languages High level languages allow the programmer to write programs without having to be as concerned about what kind of computer the program is being run on. Programs written in high level languages must be translated into a form that the CPU can understand before they can be executed. High Level Language Compiler Interpreter CPU 6
Compiler and interpreter A compiler is a program that reads code and produces a standalone executable program that the CPU can understand directly. Once your code has been turned into an executable, you do not need the compiler to run the program. An interpreter is a program that directly executes your code without compiling it into machine code first. Interpreters tend to be more flexible, but are less efficient when running programs because the interpreting process needs to be done every time the program is run. This means the interpreter is needed every time the program is run. 7
Flow of execution of a C++ Program 8
Background of C++ S.N. Year Event 1. 1979 C++ was developed by Bjarne Stroustrup at Bell Labs as an extension to C 2. 1998 Ratified by ISO 3. 2003 Two updates in C++ for additional functionality 4. 2011, 2014 The two updates were ratified respectively 9
10
Compiling your first program #include <iostream.h> { } int main() std::cout << "Hello world!" ; return 0; // To use gcc compiler open run command window // to compile the program in gcc type g++ programname.cpp //this will create one executable file "a". just type a // in the command line to execute it. //if you want to customize the executable file name you //can write the commnd g++ myfp.cpp -o myfp.o // see myp02 to myp05 to learn some basics of programming 11
Objects C++ programs create, access, manipulate, and destroy objects. An object is a piece of memory that can be used to store values. You can think of an object as a mailbox, or a cubbyhole, where we can store and retrieve information. All computers have memory, called RAM (random access memory), that is available for programs to use. When an object is defined, a piece of that memory is set aside for the object. Most of the objects that we use in C++ come in the form of variables. A variable in C++ is simply an object that has a name. 12
Initialization vs. assignment C++ supports two related concepts that new programmers often get mixed up: assignment and initialization. After a variable is defined, a value may be assigned to it via the assignment operator (the = sign): C++ will let you both define a variable AND give it an initial value in the same step. This is called initialization. 13
cout, cin, and endl std::cout: in the iostream library can be used to output text to the console. Assignment: Print your name, roll no, section, branch and college name using C++. std::cin is the opposite of std::cout -- whereas std::cout prints data to the console using the output operator (<<), std::cin reads input from the user at the console using the input operator (>>). 14
Quiz What is the difference between assembly level and high level programming language? What is the difference between a statement and expression? How will you differentiate a function from a library Which symbol is used at the end of C++ statements What is syntax error? 15
Lab Assignments 1. Write a program that prints your name, roll no, section, branch and college name in different consecutive lines using C++ programming. 2. Write a program to separate all digits of a four digit number and display sum of first and third digit. 3. Write a C++ program to calculate sum of two numbers provided by the user. 4. Calculate total income of a person using five different assets of income. 5. Evaluate the value of y in following expression using C++, if x=2, a=4,b=3 y x 2 b a a 2 x 2 16
Dr. Atul Kumar Dwivedi, BIT Durg, (India) email : atuldwivedi@live.in 8/5/2017 17