1.12k likes | 1.17k Views
Subject Name: Object Oriented Programming with C++ Subject Code: 10CS36 Prepared By: Tamilarasi, Madhuleena, Deepa Neema Department: CSE Date : 26-08-2014. Introduction to C++. Engineered for Tomorrow. Objectives. What is C++ A Simple Program Comments in C++ Output Operator
E N D
Subject Name: Object Oriented Programming with C++ Subject Code: 10CS36 Prepared By: Tamilarasi, Madhuleena, Deepa Neema Department: CSE Date : 26-08-2014
Introduction to C++ Engineered for Tomorrow
Objectives • What is C++ • A Simple Program • Comments in C++ • Output Operator • Input Operator • Cascading of I/O Operator • Structure of a C++ Program
History of C++ History of C • Evolved from two other programming languages • BCPL and B • “Typeless” languages • Dennis Ritchie (Bell Laboratories) • Added data typing, other features • Development language of UNIX • Hardware independent • Portable programs • 1989: ANSI standard • 1990: ANSI and ISO standard published • ANSI/ISO 9899: 1990
C++ • C++ is an object-oriented programming language. • Developed by Bjarne Stroustrup at AT&T Bell Laboratories, USA in the early 1980’s. • Simule 67 + C language C++ • 1997 November ANSI/ISO standards committee standardised C++. • For developing editors, compilers, databases, communication systems, etc.
Programming Techniques • Unstructured Programming • Procedural Programming • Modular Programming • Object Oriented Programming
Object Oriented Programming • Object-oriented programming solves some of the problems of the programming techniques • OOP treats data as a critical element in the program development and does not allow it to flow freely around the system • It ties data more closely to the function that operate on it, and protects it from accidental modification from outside functions • OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects • The data of an object can be accessed only by the functions associated with that object. However functions of one object can access the functions of other objects
Basic concepts of OOP • Objects, • Classes, • Inheritance, • Data Abstraction, • Data Encapsulation, • Polymorphism, • Overloading, • Reusability.
Basic concepts of OOP • Object is the basic unit of object-oriented programming. Objects are identified by its unique name. An object represents a particular instance of a class. • Classes are user defined data types based on which objects are created. Objects with similar properties and methods are grouped together to form a Class. • Inheritance is the process of forming a new class from an existing class or base class. It helps in reducing the overall code size of the program. • Data Abstraction increases the power of programming language by creating user defined data types. • Data Encapsulation combines data and functions into a single unit called Class. Itenables the important concept of data hiding possible.
Basic concepts of OOP • Polymorphism allows routines to use variables of different types at different times. An operator or function can be given different meanings or functions. Polymorphism refers to a single function or multi-functioning operator performing in different ways. • Overloading is one type of Polymorphism. It allows an object to have different meanings, depending on its context. When an exiting operator or function begins to operate on new data type, or class, it is understood to be overloaded. • Reusability : This term refers to the ability for multiple programmers to use the same written and debugged existing class of data. This is a time saving device and adds code efficiency to the language.
Include files Class declaration Member functions declaration Main function program Structure of a C++ Program Better to organize a program file into three separate files. • Class declarations in a header file. • Definition of member function in a separate file. • The main program that uses the class is placed in a third file which includes the previous two files.
A Simple Program • C++ works by giving (separate) instructions to the computer. • These instructions can be written as functions. • The primary function used in C++ is called main. This means that every C++ program should have the main() function. Because a function is an assignment, in order to perform its job, a function has a body. The body of a function starts with an opening curly bracket "{" and closes with a closing curly bracket "}".
// my first program in C++ #include <iostream.h> int main ( ) { cout << "Hello World!"; return 0; } Hello World! A Simple Program continue…
Comments in C++ • The most basic documentation you have to perform is to put comments as much as you can. Comments help you and other people who read your code to figure out what you were doing. • Comments are not read by the compiler while executing your program. • C++ supports two ways to insert comments: • // line comment • /* block comment */
Comments in C++ continue… // The hello.cpp program // Include the iostream library #include <iostream.h> int main( ) { /* Here is a simple sentence that I want to display when the program starts. It doesn't do much. I am planning to do more stuff in the future. */ cout << "Hi, this is my program!"; return 0; } // The end of my program
Variables and Data Types Tokens The smallest individual units in a program are known as tokens. • Keywords • Identifiers • Constants • Strings • Operators
Basic Data Types C++ Data Types User-defined Type Built-in Type Derived Type structure union class enumeration array function pointer reference Integral Type Void Floating Type int char float double
Identifiers continue… The name of a variable: • Starts with an underscore “_” or a letter, lowercase or uppercase, such as a letter from a to z or from A to Z. Examples are Name, gender, _Students, pRice. • Can include letters, underscore, or digits. Examples are: keyboard, Master, Junction, Player1, total_grade, _ScoreSide1. • Cannot include special characters such as !, %, ], or $. • Cannot include an empty space. • Cannot be any of the reserved words. • Should not be longer than 32 characters (although allowed).
Basic Data Types continue… ANSI C++ added two more data types • bool • wchar_t
Data Type - bool A variable with bool type can hold a Boolean value true or false. Declaration: bool b1; // declare b1 as bool type b1 = true; // assign true value to b1 bool b2 = false; // declare and initialize The default numeric value of true is 1 and false is 0.
Data Type – wchar_t The character type wchar_t has been defined to hold 16-bit wide characters. wide_character uses two bytes of memory. wide_character literal in C++ begin with the letter L L‘xy’ // wide_character literal
Built-in Data Types int, char, float, double are known as basic or fundamental data types. Signed, unsigned, long, short modifier for integer and character basic data types. Long modifier for double.
Built-in Data Types continue… Type void was introduced in ANSI C. Two normal use of void: • To specify the return type of a function when it is not returning any value. • To indicate an empty argument list to a function. • eg:- void function-name ( void )
Built-in Data Types continue… Type void can also used for declaring generic pointer. A generic pointer can be assigned a pointer value of any basic data type, but it may not be de-referenced. void *gp; // gp becomes generic pointer int *ip; // int pointer gp = ip; // assign int pointer to void pointer Assigning any pointer type to a void pointer is allowed in C & C++.
Built-in Data Types continue… void *gp; // gp becomes generic pointer int *ip; // int pointer ip = gp; // assign void pointer to int pointer This is allowed in C. But in C++ we need to use a cast operator to assign a void pointer to other type pointers. ip = ( int * ) gp; // assign void pointer to int pointer // using cast operator *ip = *gp; is illegal
Derived Data Types Arrays The application of arrays in C++ is similar to that in C. Functions top-down - structured programming ; to reduce length of the program ; reusability ; function over-loading.
Derived Data Types continue… Pointers Pointers can be declared and initialized as in C. int * ip; // int pointer ip = &x; // address of x assigned to ip *ip = 10; // 10 assigned to x through indirection
Derived Data Types continue… Pointers C++ adds the concept of constant pointer and pointer to a constant. char * const ptr1 = “GOODS”; // constant pointer int const * ptr2 = &m; // pointer to a constant
Symbolic Constants Two ways of creating symbolic constant in C++. Using the qualifier const Defining a set of integer constants using enum keyword. Any value declared as const can not be modified by the program in any way. In C++, we can use const in a constant expression. const int size = 10; char name[size]; // This is illegal in C.
Symbolic Constants continue… const allows us to create typed constants. #define - to create constants that have no type information. The named constants are just like variables except that their values can not be changed. C++ requires a const to be initialized. A const in C++ defaults, it is local to the file where it is declared. To make it global the qualifier extern is used.
Reference Variables A reference variable provides an alias for a previously defined variable. For eg., if we make the variable sum a reference to the variable total, then sum and total can be used interchangeably to represent that variable. data-type & reference-name = variable-name float total = 100; float &sum = total;
Reference Variables continue… A reference variable must be initialized at the time of declaration. This establishes the correspondence between the reference and the data object which it names. int x ; int *p = &x ; int & m = *p ;
Reference Variables continue… void f ( int & x ) { x = x + 10; } int main ( ) { int m = 10; f (m); } When the function call f(m) is executed, int & x = m;
Operators in C++ • New Operators in C++ • Scope Resolution Operator • Memory Management Operator • Manipulators • Type Cast Operator
All C operators are valid in C++ New operators in C++ : • << Insertion Operator • >> Extraction Operator • : : Scope Resolution Operator • : : * Pointer-to-member declaration • ->* Pointer-to-member Operator • .* Pointer-to-member Operator • delete Memory Release Operator • endl Line Feed Operator • new Memory Allocation Operator • setw Field Width Operator
C++ is a block structured language. The scope of a variable extends from the point of its declaration till the end of the block containing the declaration. A variable declared inside a block is said to be local to that block. ……… ……… { int x = 10; ……… ……… } ……… ……… { int x = 1; ……… ……… } Scope Resolution Operator
Blocks in C++ are often nested. In C, the global version of a variable can not be accessed from within the inner block. C++ resolved this problem with the use of the scope resolution operator ( :: ). ……… ……… { int x = 10; ……… Block1 ……… { int x = 1; ……… Block 2 ……… } ……… ……… } Scope Resolution Operator continue…
Scope Resolution Operator continue… The scope resolution operator ( :: ) can be used to uncover a hidden variable. : : variable-name This operator allows access to the global version of a variable.
Scope Resolution Operator continue… #include<iostream.h> #include<conio.h> intm= 10; void main( ) { intm= 20; clrscr( ); cout<<"m_local = "<<m<<"\n"; cout<<"m_global = "<<::m<<"\n"; getch( ); } m_local = 20 m_global = 10
Output Operator cout << "Hi, this is my program!"; cout is a predefined object that represents the standard output stream in C++, here the standard output stream is screen. screen << cout C++ variable Object Insertion Operator
screen << cout C++ variable Output Operator continue… • The operator << is called insertion or put to operator. • It inserts ( or sends ) the contents of the variable on its right to the object on its left. cout << "Hi, this is my program!"; cout << number ; Object Insertion Operator
Input Operator cin >> number1; cin is a predefined object that corresponds to the standard input stream in C++, here the standard input stream is keyboard. Object Extraction Operator C++ variable >> cin 50.55 Keyboard
Input Operator continue… Object Extraction Operator C++ variable • The operator >> is known as extraction or get from operator. • It extracts ( or takes ) the value from the keyboard and assigns it to the variable on its right. >> cin 50.55 Keyboard
Cascading of I/O Operators • Multiple use of << or >> in one statement is called cascading. eg:- cout << “Sum = ” << sum << “\n”; cin >> number1 >> number2;
Manipulators Manipulators are operators that are used to format the data display. Commonly used manipulators are: • endl // causes a line feed when used in an // output statement • setw // to specify field width and force the // data to be printed right-justified
Manipulators continue… #include<iostream.h> #include<conio.h> #include<iomanip.h> void main( ) { intm,n,p; m = 2597; n = 14; p = 175; clrscr( ); cout<<setw(10)<<"First = "<<setw(10)<< m <<endl <<setw(10)<<"Second = "<<setw(10) << n <<endl <<setw(10)<<"Third = "<<setw(10) << p <<endl; getch( ); } First = 2597 Second = 14 Third = 175
Type Cast Operator C++ permit explicit type conversion of variables or expressions using the type cast operator. • (type-name) expression // C notation • type-name ( expression ) // C++ notation // like a function call // notation eg:- average = sum /(float) i; // C notation average = sum / float(i); // C++ notation
Expressions and Their Types • Constant Expressions • Integral Expressions • Float Expressions • Pointer Expressions • Relational Expressions • Logical Expressions • Bitwise Expressions An expression may also use combination of the above expressions – Compound expressions.