Basic of C++
Basic of C++
Include headers
#include< headerfile>
Comma headers
iostream , istream , math , cctype , String
Name space
Using namespace std
Datatypes
int , char , float , double , void , bed
Comments
//comments text
/* Multiline comment text */
Arithmetic operator
+(Addition) , -(Subtraction) , *(Multiply) , /(Division)
Relational operators
<=(less than or equal to) , < (less than) , > (Greater than) , >= (greater than or equal to) , ==(equal to equal to)
Logical operator
׀׀ (logical OR) , && (logical AND) , ! (logical NOT)
Pointers
int *ptr // Define Pointer
ptr = &var // Ptr set to address var
var 2 = *ptr // Set Var e, to value of var
For loop
For (<initialize>; <Condition>)
{ <statement >;}
While loop
while (< condition >
{ <statement >;}
If else
if (< condition>)
{ <statement 1 > ; }
else
{ <statement 2 > ; }
Do while loop
do {< statement > ; }
while (< condition>);
Array
//New 5 element Array
int my array [5];
//Array index starts at 0
//Access third element
my array [2] = var ;
Switch Statement
Switch (<expression>)
}
case <constant 1 >;
<statements sequence 1 > ;
break ;
case <constant 2 > ;
<statement sequence 2 > ;
break ;
case < constant n+1 > ;
<statement sequence n+1 > ;
break ;
{default :
<Statement sequence n > ;
}
I/O operators
>> //input operator
<< // output operator
<in >> var1 , var2 , var3 ;
cout <<"Test : "<<var1 << var2 <<end ;
c in.get (char buffer.streamsize numchart );
File I/O
fstream file
file.open (filename "<<file mode constant >);
//read and write like cin and cout
file >> var ;
file <<"Text " <<end ;
//read entine line
getline (file , line );
//Reading and writing binary data
file.read (memory-black ,size );
file.read (memory-black , size);
file.close ();
File mode Constant
ios ::in // opens file for reading
ios ::out // opens file for writing
ios ::ate // seeks FOF ,I/O operations can occur
ios ::app // causes output to be appended at FOF
ios ::trunc // destroys previous contents
ios ::nocreate // causes open c) to fail if doesn't exist
ios ::noreplace // causes open c) to fail if already exist
Function prototype
<return-data-type><function-name> (permanent list>
{body of function}
Class prototype
class <class-name >
{
public :
//method-prototypes
protected :
//method-prototypes
private :
// method - prototypes
// data - attributes }
Structure prototype
struct <structure name >}
member - type 1 member-name 1;
member -type 2 member-name 2;
} <object-name>;
Accessing data structure
//Access member variable from struct / class
mystruct.membervar 1 = var ;
//call class method
myclass.method 1(args);
//pointer to struct /class
mystructType *ptr ;
ptr = &mystruct ;
ptr -> membervar1 = var ;
Comments
Post a Comment