Sie sind auf Seite 1von 6

C++

=========
C++ basics OOPs
=============================
It is necessary to understand some of the concepts used extensively
in object-oriented programming. These include:
=> Objects
=> Classes
=> Data abstraction and encapsulation
=> Inheritance
=> Polymorphism
=> Dynamic binding
=> Message passing

Hello world C++


---------------#include<iostream>
Using namespace std;
int main()
{
cout<< c++ is better than c \n ;
return 0;
}
Note:
-----=> Namespace ANSI C++ standards
=> This defines a scope for the identifiers that are used in a program.
For using the identifier defined in the namespace scope we must
include the using directive, like
Using namespace std;
=> Here, std is the namespace where ANSI C++ standard class
libraries are defined. All ANSI C++ programs must include
this directive.
This will bring all the identifiers defined in std to the current global
scope.
Using and namespace are the new keyword of C++.
Example: adding two no
-------------------#include<iostream.h> // include header file
Using namespace std;
Int main()

{
Float number1, number2,sum, average;
Cin >> number1; // Read Numbers
Cin >> number2; // from keyboard
Sum = number1 + number2;
Average = sum/2;
Cout << Sum = << sum << \n ;
Cout << Average = << average << \n ;
Return 0;
}

Note:
extraction operator : >>
insertion operator : <<

Example with Class


-------------------#include<iostream.h> // include header file
using namespace std;
class person
{
char name[30];
Int age;
public:
void getdata(void);
void display(void);
};
void person :: getdata(void)
{
cout << Enter name: ;
cin >> name;
cout << Enter age: ;
cin >> age;
}
Void person : : display(void)
{
cout << \nNameame:
<< name;
cout << \nAge:
<< age;
}
int main()
{
person p;
p.getdata();
p.display();
Return 0;
} //end of example

Structure of C++ Program


--------------------------1. Include Files
2. Class declaration
3. Member functions definitions
4. Main function program

C++ Function Definition and Declaration


===========================================
int sum(int a, int b)
{
return a+b;
}
main(){
........
}

CONSTANT METHOD ARGUMENTS


-------------------------Void max(const float x, const float y, const float z); //const should n
ot be modified by the function max ()
CALLING FUNCTIONS
-------------------(a) Value
(b) Reference

Call by Value
-------------int main(){

Void calculate (float, float, float);


......
}

Void calculate (float p, float r, float t)


{
Float interest; //local variable
Interest = p* (pow((1+r/100.0),t))-p;
Cout<< \nCompound interest is : <<interest;
}

Call by Reference
---------------------int main()
{
void swap (int &, int &); //function prototype
.......
........
swap(num1,num2); //function call
}
void swap (int & a, int & b)
{
int temp=a;
a=b;
b=temp;
}

INLINE FUNCTIONS
---------------Eg:
inline void min (int x, int y)
cout<< (x < Y? x : y);
}

The inlining does not work for the following situations :


------------------------------------------------------------1. For functions returning values and having a loop or a switch
or a goto statement.
2. For functions that do not return value and having a return st
atement.
3. For functions having static variable(s).

4. If the inline functions are recursive (i.e. a function define


d in terms of itself).
The benefits of inline functions are as follows :
--------------------------------------------------------1. Better than a macro.
2. Function call overheads are eliminated.
3. Program becomes more readable.
4. Program executes more efficiently

SCOPE RULES OF FUNCTIONS AND VARIABLES


----------------------------------------In C++ there are four kinds of scope as given below :
-----------------------------------------------------1. Local Scope
2. Function Scope
3. File Scope
4. Class Scope

Ex: C
Class student
{
private:
char reg_no[10];
char name[30];
int age;
char address[25];
public :
void init_data()
{
- - - - - //body of function
- - - - }
void display_data(){}
};
Inside main
-------------student ob; //class variable (object) created
- - - - - - - - -

Ob.init_data(); //Access the member function


ob.display_data(); //Access the member function
- - - - - - - - -

Das könnte Ihnen auch gefallen