BasicC++
Transcript of 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
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.
Special thanks to :
The Management, Principal and Head, Dept. of Computer Science
of P.E.S School of Engineering, , Bangalore, Karnataka, India.
Advantage of attending this seminar
• Primarily, my friendship • Learn and understand the
advantage of OOT• Learn basic concepts of OOP
A priori knowledge required ?
• Knowledge of basic C programming language
• Knowledge of compiling source files
Let us start with a Q ?
What is the major difference between C and C++ ?
• Inclusion of OO concepts to C++
• C is a POPL
• C++ is a OOPL
What is Procedure Oriented
Programming ?
• Try to solve a problem keeping procedures in mind
• Data insecurity is a major problem
Data Pollution !!!struct Library void Login( ){ { ……………….. Lib.Books = 7; //Dangerous
int Books ; } …………………};struct Library Lib;int main( ){ ……………………
Login( );………………………
}
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
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”
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.
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( );
OOP concepts in C++
• Encapsulation• Polymorphism• Inheritance• Abstraction
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
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
}
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( );}; }; };
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 );
}
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;
}
#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;
}
How to access global data in C++ ?
#include <iostream>using namespace std;int a = 6;int main( ){
int a = 7;cout << a << endl;
}
• Using scope resolution operator [::]
#include <iostream>using namespace std;int a = 6;int main( ){
int a = 7;cout << ::a << endl;
}
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( )
}
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 );
}
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; }
Guess the output !!
#include <iostream>using namespace std;void Func( ){
cout << “void” << end;} int Func( ){
cout << “int” << endl;} int main( ){
Func( );}
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
}
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
}
Puzzle Test
Answer my puzzle and take away the prize !!!
Any Queries ?
Thank you !!!!
Contact me ?
• [email protected] (General)• [email protected] (Technical)• Facebook (Subhash Ku)• Orkut (Subhash HMSIT)• Mobile: +91 9845456000