Sei sulla pagina 1di 18

What is C++???

C++ is a general-purpose Programming language with a bias towards systems programming that is a better than C, supports data abstraction, supports object-oriented programming, and Supports generic programming. With C++ you have detailed control over objects, including their creation, destruction, inheritance and access to pointers. You can add new features by defining classes and overloading operators. It is for the programmer who demands and deserves the worlds most powerful programming language.

Function
Function Prototyping: The prototype describes the function interface to the compiler by giving details such as the number of arguments and the type of return values. Function prototype is of the following form: Type function_name(argument _list); Example: float sqr(int x,inty); float sqr(int ,int ); Inline Function: Inline function is used to save some memory space and save time for execution.It makes the programe faster.But when a function is small, a substantial percentage of execution time may be spent in such overloads Declaration: inline function_header { Function body } Function Overloading: Overloading refers to the use of same thing for different purposes. It means that we can use the same function name to create functions that perform a variety of different tasks.

Class : A class is a way to bind the data and its associated function together. It allows the data to be hidden, if necessary, from external use. The general form of class declaration: Class class- name { Private: Variable declaration; Function declaration; Public: Variable declaration; Function declaration; }; Accessing class member: The private data of a class can be accessed only though the member functions only of that class. The main () cannot accessed the private member. But the public members are accessed by objects created in main() through dot operator .

Classes and Objects

Defining Member Function: Outside the class definition: Return-type class-name :: function-name (argument-list) { Function body } Inside the class definition: Class item { int number; Float cost; Public: Void get-data(int a, float b); Void put-data(void) { Cout<<number; Cout<<cost; } };

Objects as Function Arguments: A object of a class can be used as function argument. This can be done in two ways: i. A copy of the entire objects is passed to the function. ii. Only the address of the object is transferred to the function. The first method is called pass-by-value. The second method is called pass-byreference. Example:
#include<iostream.h> #include<conio.h> class time { int hours,minutes; public: void gettime(int h,int m) { hours=h;minutes=m; } void puttime(void) { cout<<hours<<"hours and"; cout<<minutes<<"minutes\n"; }

void sum(time,time); }; void time::sum(time t1,time t2) { minutes=t1.minutes+t2.minutes; hours=minutes/60; minutes=minutes%60; hours=hours+t1.hours+t2.hours; } int main() { time T1,T2,T3; T1.gettime(2,45); T2.gettime(34,57); T3.sum(T1,T2); cout<<"t1=" ;T1.puttime(); cout<<"t2=" ;T2.puttime(); cout<<"t3=" ;T3.puttime(); getch(); return 0; }

Friend Function: A friend is not member function but it has ability to access private data members of any class. A friend of the class as shown below: class abc { . .. Public: .. Friend void xyz(void); }; Characteristics of friend function: 1. It is not in the scope of the class to which it has been declared as friend. 2. since it is not in the scope of the class.it can not be called by using the object of that class. 3. It can be invoked like normal function without the help of any object. 4. It can be declared either in the public or Private part of a class without affecting its meaning 5.It has the objects as argument.

Constructors and Destructors:

A constructor is a special member function of class name whose function is to initialize the objects of its class. The is invoked whenever an object of its associated class is created.
Definition of constructor: Class integr { int m,n; public: integer(void); .. }; Integer::integer(void) { m=0; n=0; } Characteristics of constructor: 1.They should be declared in the public section. 2.They are invoked automatically when the objects are created. 3.They can not have return type,not even void and therefore they cannot return values. 6.Constructor cannot be virtual.

Constructor calling: 1. Explicit calling 2. Implicit calling. Example:6.1

#include<iostream.h> #include<conio.h> class integer { int m,n; public: integer(int,int); void display(void) { cout<<"m="<<m<<"\n"; cout<<"n="<<n<<"\n"; } }; integer::integer(int x,int y) {m=x;n=y;} int main() { integer int1(0,100); integer int2=integer(25,75); int1.display(); int2.display(); getch(); return 0; }

Destructor: A destructor is used to destroy the objects created in a constructor. Destructor is a member function whose name is the same as the class name but is preceded by a tilde sign. It never takes any value nor does it return value. It just releases some memory for future use. Example:6.7
#include<iostream.h> #include<conio.h>
int count=0; class alpha { public: alpha() { count++; cout<<"\n no. of object created:"<<count; } ~alpha() { cout<<"\n no. of object destroyed:"<<count; count--; }

};

int main() { { alpha A1,A2,A3; } getch(); return 0; }


Operator Overloading: Operator Overloading:Operator overloading provides a flexible option for the creation of new definitions for most of the c++ operators. We can overload all the entire C++ operator except the following: 1. Class member access operators (.,.*). 2. Scope resolution operator(::). 3. Size operator (sizeof). 4. Conditional operator(?:).

Defining Operator Overloading: return-type class-name::operator op (arglist) { Function body }


Here operator op is the function name and op is the operator. Operator function may be either member function or friend function. In friend function one argument is used for unary operator and two for binary operator. But in member functions no argument for unary and one for binary operator.

Inheritance:

The mechanism of deriving a new class from an old class is known as inheritance. The old class is referred to as base class and the new one is called the derived class. Types of inheritance: 1.Single 2.Multilevel 3.Miltiple 4.Hierarchical and 5.Hybrid inheritance. Defining Derived Class: Class derived-class-name :visibility-mode base-class-name { member of derived class; }; Single inheritance : A derived class with only one base class is called single inheritance. Example:
#include<iostream> #include<conio.h> class days { long int n; public : long int getdays(); }; long int days :: getdays() { cout<<"Enter days : "; cin>>n;

return n; } class calculation { long int day, month, year; public : void cal() { long int t; days d; t = d.getdays(); year = t / 365 ; t = t % 365 ; month = t / 30; day = t % 30; cout<<"\nYear = "<<year<<"\n"<<"Month = "<<month<<"\nDay = "<<day<<"\n\n"; } }; class result : public calculation { public : void show() {cal();} }; int main() { result r; r.show(); getch(); return 0;}

Multilevel Inheritance: The mechanism of deriving class from another derived class is known as multilevel inheritance. Defining a Multilevel inheritance: Class A{}; Class B : public A {}; Class C : public B{..}; Multiple Inheritances: A derived class with more than one base class is called multiple inheritance. Defining a multiple inheritances: Class D : visibility B-1,visibility B-2 { Body of D }; Hybrid inheritance: Hybrid inheritance is combination of multiple and multilevel inheritance.

All the data are stored in hard disk using the concept of file.A file is a collection of related data stored in a particular area on the disk. Opening a File: A file can be opened in two ways: Opening files using constructor: While opening files using constructor, we need to pass the desired file name as a parameter to the constructor. This involves the following steps: 1. Create a file stream object to manage the stream using the appropriate class. 2. Initialize the file object with the desired filename. For example,: ofstream outfile ("results"); Opening files using open() The open() function can be used to open multiple files that use the same stram object. This is done as follows: filestream class stream-object; stream-object.open("filename"); We can close a file in the following process: outfile.close();

Working With File

THANKS TO ALL

18

Potrebbero piacerti anche