BasicC++

31
A Technical Talk A Technical Talk on C++ on C++ by by Subhash.K.U Subhash.K.U @ @ P.E.S School of P.E.S School of Engineering, Engineering, Bangalore Bangalore

Transcript of BasicC++

Page 1: BasicC++

A Technical Talk on A Technical Talk on C++ C++

bybySubhash.K.USubhash.K.U

@@P.E.S School of Engineering,P.E.S School of Engineering,

BangaloreBangalore

A Technical Talk on A Technical Talk on C++ C++

bybySubhash.K.USubhash.K.U

@@P.E.S School of Engineering,P.E.S School of Engineering,

BangaloreBangalore

Page 2: BasicC++

About the speaker

Subhash.K.U currently works as Senior Software Engineer at Mascon Global Ltd, Bangalore. He is also a consultant for Motorola Solutions, Bangalore. He is the founder chairman and chief-facilitator at TeachGuru Foundation,Bangalore.

Page 3: BasicC++

Special thanks to :

The Management, Principal and Head, Dept. of Computer Science

of P.E.S School of Engineering, , Bangalore, Karnataka, India.

Page 4: BasicC++

Advantage of attending this seminar

• Primarily, my friendship • Learn and understand the

advantage of OOT• Learn basic concepts of OOP

Page 5: BasicC++

A priori knowledge required ?

• Knowledge of basic C programming language

• Knowledge of compiling source files

Page 6: BasicC++

Let us start with a Q ?

What is the major difference between C and C++ ?

Page 7: BasicC++

• Inclusion of OO concepts to C++

• C is a POPL

• C++ is a OOPL

Page 8: BasicC++

What is Procedure Oriented

Programming ?

• Try to solve a problem keeping procedures in mind

• Data insecurity is a major problem

Page 9: BasicC++

Data Pollution !!!struct Library void Login( ){ { ……………….. Lib.Books = 7; //Dangerous

int Books ; } …………………};struct Library Lib;int main( ){ ……………………

Login( );………………………

}

Page 10: BasicC++

What is Object Oriented

Programming ?• Programmers think in terms of

Objects

• Can model real world entities

• An object occupies space, has an identity and also has behaviour

Page 11: BasicC++

How do you store integer numbers ?

– use “int a = 6; “

How do you store the real world entity “Book” while designing a Library Management System ?– use “User defined Objects“– No built in data type to store the real

world entity “Book” or “Pen” or “Employee”

Page 12: BasicC++

How to create user-defined objects ?

– In C :• struct Book {

char name;int pages;double price;

} ;struct Book sku;sku.name = ‘s’; int a;sku.pages = 469; a = 6;sku.price = 335.00;

Disadvantage:• Cannot include related function within a structure. Or

cannot encapsulate.

Page 13: BasicC++

In C++:

– struct Book { private: char name; state of an object

int pages; double price;

public: void GetBookDetails( ) behaviour of an

object {

//implementation details }

}; struct Book sku; occupies space and has an identity sku.name = ‘s’; sku.pages = 369;

sku.price = 335.00sku.GetBookDetails( );

Page 14: BasicC++

OOP concepts in C++

• Encapsulation• Polymorphism• Inheritance• Abstraction

Page 15: BasicC++

Encapsulation

• Shielding the data and functions that operate on that data together into one unit and protecting the data access from outside world is known as Encapsulation

Page 16: BasicC++

Polymorphism• An entity in different forms• Example: int main( )

{int a[5] = { 0, 1, 2, 3, 4 };double b[5] = {2.2, 3.3, 5.5, 6.6, 2.0 };Add( a); // Add_int(a); in CAdd (b ); // Add_double(b); in C

}

Page 17: BasicC++

InheritanceReusing the existing code by inheriting the characteristics of the

base class by a derived class.class Shape{ Draw( );};

class Square class Circle class Rectangle { { {

Draw( ); Draw( ); Draw( );}; }; };

Page 18: BasicC++

Abstraction• Hiding implementation details.• Templates used to implement abstractionExample

int main( )

{int a[5] = { 4, 1, 2, 3, 0, 5 };char *name[3] = { “Subhash”, “Aishwarya”, “Yusuf

Pathan” };sort( a );sort( name );

}

Page 19: BasicC++

A simple C++ program• #include <iostream> using namespace std; int main( )

{int a = 6;cout << a << endl;int b;cin >> b;cout << b;cout << “Subhash\n”;return 0;

}

Page 20: BasicC++

#include <iostream> int main( ){

int a = 6;std::cout << a << std::endl;int b;std::cin >> b;std::cout << b;std::cout << “Subhash\n”;return 0;

}

Page 21: BasicC++

How to access global data in C++ ?

#include <iostream>using namespace std;int a = 6;int main( ){

int a = 7;cout << a << endl;

}

Page 22: BasicC++

• Using scope resolution operator [::]

#include <iostream>using namespace std;int a = 6;int main( ){

int a = 7;cout << ::a << endl;

}

Page 23: BasicC++

A simple class and an its objects#include <iostream>using namespace std;class Example{

private:int a; //Data member

public:int b; // Data membervoid Set_Data( int x ) { a = x }; // member functionvoid Get_data( ) { cout << a << b << endl; } //member function

}; int main( ) {

Example e;e.a = 6; // result in compilation errore.b = 7; // legal statemente.Set_Data( 5 ); e,\e.Get_Data( )

}

Page 24: BasicC++

Function Overloading#include <iostream>using namespace std;int Add( int x, int y ){

return ( x + y );} double Add( double x, double y ){

return ( x + y );}int main( ){

Add( 5, 6);Add( 5.5, 1.1 );

}

Page 25: BasicC++

Inline Functions#include <iostream>using namespace std;inline Add( int x, int y ){

return (x + y );} int main( ){

int res = Add( 1, 2 ); // int res = x + y; }

Page 26: BasicC++

Guess the output !!

#include <iostream>using namespace std;void Func( ){

cout << “void” << end;} int Func( ){

cout << “int” << endl;} int main( ){

Func( );}

Page 27: BasicC++

Default Arguments#include <iostream>using namespace std;void Add( int, int, int = 1 );void Add( int x, int y, int z ){

cout << (x + y + z) << endl;} int main( ){

Add( 5, 6, 7); // CorrectAdd( 5, 6 ); // Correct Add(5); // Error

}

Page 28: BasicC++

Guess the output#include <iostream>using namespace std;void Add( int, int = 9, int );void Add( int x, int y, int z ){

cout << (x + y + z) << endl;} int main( ){

Add( 5, 6, 7); // CorrectAdd( 5, 6 ); // Correct Add(5); // Error

}

Page 29: BasicC++

Puzzle Test

Answer my puzzle and take away the prize !!!

Page 30: BasicC++

Any Queries ?

Thank you !!!!

Page 31: BasicC++

Contact me ?

[email protected] (General)• [email protected] (Technical)• Facebook (Subhash Ku)• Orkut (Subhash HMSIT)• Mobile: +91 9845456000