C++ basics Getting started with, and Data Types pm_jat@daiict.ac.in
Recap Last Lecture We talked about Variables - Variables, their binding to type, storage etc., Categorization based on storage binding Scope and life time of variables Also talked about Type Errors, Type Checking and Strongly Typed Languages
Getting Started with C++ Getting Started with C++ is easy. In principal every C program is valid C++ program too; C++ is superset of C. Here is the simple C++ program..
C++ Program This is C++ Program Any C program is also a valid C++ program! At this point you shall not find much difference in C and C++ except- C++ has different way of including header files. cin and cout using names std; endl
Note the syntax (difference) of Include using namespace Input/Output cin and cout
#include C++ introduces concept of namespaces. This is a mechanism to avoid name collisions from several developers a large number of developers are contributing toward developing various C++ libraries All C++ language library has been placed under namespace std We will talk about namespaces little later; till then just use it as such. #include <iostream> using namespace std;
#include For most old C header files, prefix c to header file name and drop.h #include <cstring> #include <cmath> #include <cstdio> #include <cstdlib> By the way what header file does?
Input and Output in C++ You can still use scanf and printf, include <cstdio> for this, recall all c library is available in C++ However you use cin and cout objects, wrapping stdin and stdout devices of C, respectively cin >> a >> b; cout << "Average= " << c << endl; cin operator is called as extraction operator, while cout is called as insertion operator! You also have cerr, object wrapper around stderr device of C More about input/output sometime later
Data Types A data type defines a collection of data values and a set of predefined operations on those values Can be categorized in, Primitive Data Types, and Abstract Data Types (or programmer defined data types) Primitive data types are also Abstract Data Types in precise sense (though we never call so), Most languages treat non-primitive data types differently. In C++, string and vector are most commonly used such data types
Primitive Data Types in C++ Following are Primitive Data types supported in C++ Integer: short, int, long Floating Point : float, double, long double Character: char Boolean: bool A quick overview is available athttp://www.cplusplus.com/doc/tutorial/variables.html
Strings in C++ One way is, you can use same old char array, however there are certain issues here..? Array bound problem use of functions for manipulation is not like a abstract data type C++ provides string Abstract Data Type, and you can use it the way you use any primitive data type string is a class, and you need to include <string> to use it
Operations on string Declare and initialize string s1 = "Welcome"; Assign s2 = s1; s1 = "Hello"; Concatenation string s3 = s1 + s2; More operations int n = s.length(); string student_id = "200601212"; string prog_code = student_id.substr(4,2); Compare this with the way you do this with char array Note that here string is a class and s,s1,s2 are objects of that class.
Variables and Objects Here is how we create a variable int x; Here is how we create object string name= Amit ; What is difference between? In principal.. no difference; both are instance of Abstract data Types; in imperative language we call variables, while in object oriented we call objects. However; the most OOP programming languages deal primitive data types and classes differently; and typically use of term variable for instances of primitive data types while objects for instances of classes
Thanks