1 / 79

C++ Tutorials

C++ Tutorials. A.V.H.S.Prasad. What is C++?. C++ is an object oriented programming language. C++ is an extension of C with a major addition of the class construct feature. C++ is superset of C.

colin
Download Presentation

C++ Tutorials

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Tutorials A.V.H.S.Prasad

  2. What is C++? • C++ is an object oriented programming language. • C++ is an extension of C with a major addition of the class construct feature. • C++ is superset of C. • Features like classes, inheritance, function overloading, and operator overloading make C++ a truly object-oriented language. • OO features in C++ allow programmers to build large programs with clarity, extensibility and ease of maintenance, incorporating the spirit and efficiency of C.

  3. Hello World! /* Simple hello world program*/ # include <iostream>// This is include directive using namespace std; int main() { cout << “Hello World!”; //C++ statement return 0; }

  4. A Simple Program • Average of numbers. • It will take two numbers from the console and print the average of the two numbers.

  5. Tokens • Keywords • Identifiers • Constants • Strings • Operators

  6. Tokens • Keywords • Identifiers • Constants • Strings • Operators

  7. Keywords • Some words are reserved for implementing the specific C++ language features. • asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register,reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while

  8. Tokens • Keywords • Identifiers • Constants • Strings • Operators

  9. Identifiers • Identifiers refer to the names of variables, functions, arrays, classes , etc. created by the programmer. • Rules for creating it. • Only alphabet characters, digits and underscore are permitted. • The name cannot start with digit. • Uppercase and lowercase letters are distinct. • A declared keyword cannot be used as a variable.

  10. Basic Data Types

  11. Size and range of C++ basic data types

  12. Program: Math Operations • This program will take different values from console and perform various mathematical operation on these values and print the result to the console.

  13. User-Defined Data Types • Structures and Classes • Basis for OOP. • Classes enable to combine data and procedures. • Enumerated Data Type • It provides a way to attaching names to the numbers • Increase comprehensibility of the code. • Alternative mean for creating symbolic constants • Enumerates a list of words by assigning them values 0,1,2 and so on. enum shape {circle, square, triangle}; enumcolour {red, blue=4, green=8};

  14. Derived Data Types • Arrays • Values of similar type stored in continuous memory locations. • int a[10]; char string[3]=“xyz”; • Functions • Set of statements to perform specific tasks • Pointers • Special variables to store the memory location of other variables. • Used in referencing memory. • Concept of constant pointer introduced in c++.

  15. Tokens • Keywords • Identifiers • Constants • Strings • Operators

  16. Constants (literal) • Refer to fixed values that do not change in thw execution of the program. • 123 //decimal integer • 12.34 //floating point integer • 037 //octal integer • 0x2 //Hexa decimal • “C++” //string constant • ‘A’ //character constant • L’ab’ //wide-character constant

  17. Constants(Symbolic) • Using the qualifier const • const float pi = 3.14; • Using enum • enum{x,y,z}; • enum{x=200,y=300,z=400}; • enum{off,on};

  18. Tokens • Keywords • Identifiers • Constants • Strings • Operators

  19. Strings • Variables that can store non-numerical values that are longer than one single character are known as strings. • The C++ language library provides support for strings through the standard string class.

  20. Tokens • Keywords • Identifiers • Constants • Strings • Operators

  21. Operators

  22. Expressions and Their Types • Constant expressions • Integral expressions • Float expressions • Pointer expressions • Relational expressions • Logical expressions • Bitwise expressions

  23. Control Structures • The if statement

  24. Control Structures • The switch statement

  25. Control Structures

  26. Control Structure • The do-while statement

  27. Control Structures • The while statement

  28. Control Structures • The for statement

  29. Hands on

  30. Functions • A piece of code that perform specific task. • Introduces modularity in the code. • Reduces the size of program. • C++ has added many new features to the functions to make them more reliable and flexible. • It can be overloaded.

  31. Functions • Function declaration • return-type function-name (argument-list); • void show(); • float volume(intx,floaty,float z); • Function definition return-type function-name(argument-list) { statement1; statement2; } • Function call • function-name(argument-list); • volume(a,b,c);

  32. Functions • The main function • Returns a value of type int to the operating system.

  33. Program: Calculation of Volume • This program will take the values of length, breadth and height and prints the volume of the cube. It uses the function to calculate it.

  34. Functions • Parameter Passing • Pass by value • Pass by reference

  35. Functions • Parameter passing • Return by reference

  36. Function Overloading • C++ allows to use the same function name to create functions that perform a variety of different tasks. • This is know as function polymorphism in OOP.

  37. Structures • Structures Revisited • Makes convenient to handle a group of logically related data items. struct student //declaration { char name[20]; introll_number; float total_marks; }; struct student A;// C declaration student A; //C++ declaration A.roll_number=999; A.total_marks=595.5; Final_Total=A.total_marks + 5;

  38. Structures • Limitations • C doesn’t allow it to be treated like built-in data types. struct complex{float x; float y;}; struct complex c1,c2,c3; c3=c1+c2;//Illegal in C • They do not permit data hiding.

  39. Structures in C++ • Can hold variables and functions as members. • Can also declare some of its members as ‘private’. • C++ introduces another user-defined type known as ‘class’ to incorporate all these extensions.

  40. Classes and Objects • Class is a way to bind the data and procedures that operates on data. • Class declaration: class class_name { private: variable declarations;//class function declarations;//members public: variable declarations;//class function declarations;//members };//Terminates with a semicolon

  41. Classes and Objects • Class members that have been declared as private can be accessed only from within the class. • Public class members can be accessed from outside the class also. • Supports data-hiding and data encapsulation features of OOP.

  42. Classes and Objects • Objects are run time instance of a class. • Class is a representation of the object, and Object is the actual run time entity which holds data and function that has been defined in the class. • Object declaration: class_name obj1; class_name obj2,obj3; class class_name {……}obj1,obj2,obj3;

  43. Classes and Objects • Accessing class members • Object-name.function-name(actual-arguments); • obj1.setdata(100,34.4); • Defining Member Functions • Outside the class definition. return-type class-name::function-name (argument declaration) { Function body; } • Inside the class definition. Same as normal function declaration.

  44. An example: Classes and Objects

  45. Classes and Objects • Object as arrays class employee { char name [30]; float age; public: void getdata(void); void putdata(void); }; employee manager[3];//array of manager employee worker[75];//array of worker Manager[i].putdata();

  46. Program: Array of Objects • This program will create an array, takes the values for each of the element and displays it.

  47. Passing Objects in Functions • An object may be used as a function arguments.

  48. Program: Addition of time • This program creates a ‘time’ class in hour and minute format. There is a function called ‘sum’ performs the addition of time of two object and assign it to the object that has called the function.

  49. Friendly Function • Friend function are used in the situation where two classes want to share a common function. • Scientist and manager classes want to share the function incometax(). • C++ allows the common function to be made friendly with both the classes, thereby allowing the access to the private data of these classes. This function need not to be the member function of any of these classes.

  50. Friend Function Characteristics • It is not in the scope of the class, hence it cannot be called using the object of any class. It can be invoked normally. • It cannot access member names directly, but with the help of an object. Eg obj1.x • It can be declare either in the public or the private part of a class without affecting its meaning. • Usually, it has the objects as arguments.

More Related