Posts

Showing posts with the label cpp

c++ file handling

Image
File Handling in c++ In this article, you will learn about file handling in the cpp language, How to create the file in c++, How to open in c++, How to write,and How to close in c++. Why we need file handling in c++? Storing the data in the device or anywhere is important. In the same way cpp provide the facility to store the data in the files. After storing data, stored data is used as an instruction. Files are used to store the data on the device permanently. How to use file handling in c++? C++ provides the three following classes to perform the input and output operation from the files => of stream => ifstream => fstream We already used the istream and ostream for cin and cout in the program. In the same way,ifstream and ofstream is used for the input and output from the file. The only difference between iostream and fstream is that fstream is to

Template function template class in C++

Image
  C++ Template In this article of the cpp programming, we will discuss template, its functions, and its classes. In generic programming, it is a very powerful and important part of the cpp language. Why template used? For generic programming,this allows the programmer to use one code for different types rather than write the same code for the different data types. This feature makes it powerful, the code is reusable and flexible. Let's understand the concept: Template function: The function is not different from the regular function but only one difference. It works for different data types. The regular function works with only a specific type. For different data types, regular function again define for different data types. The template function defines only one time and uses for different data types.you can do the same work with less code. How to declare the function template? Keyword: Template The keyword used at the start, then followed by parameters inside the ( ) with the

virtual , pure virtual function in cpp

Image
Virtual function in cpp In this article, you will learn about the virtual function, why it is needed, and how to use it. The member function of the base class redefined in the derived classes is known as a virtual function. Why we need for virtual function in cpp? In the inheritance of the classes, we may use functions with the same name in different classes. when we use these functions, we call the function by creating the object or pointers of the classes. Example of virtual function: #include <iostream> using namespace std; class baseclass { // ctreate the base public: void display(){ //define and declare the display function cout<" we are in the Parent"<<endl; } }; class derivedclass: public baseclass{ //create derived public: void display() { //define and declare the display again cout<<" we are in the derived"<<endl; } }; class derivedclass2: public baseclass{

Function overriding in c++

Image
 Function overriding In this article, you will learn about the function overriding in cpp programming and how to access the function of the base class. Inheritance allows the programmer to use class (base class)to produce the derived class. All features of the base class transfer to the derived class.  Let suppose a member function of the base class and derived also has the same function with the same name and same argument then what will happen? If we create the object of the derived class and call the function. The compiler calls the function of the derived class and ignores the function of the base class. This property of the inheritance is known as function overriding in C++ language.

C++ Multiple,multi level,hierarchical inheritance

Image
  The most important concept in object-oriented programming is inheritance. G enerally, Inheritance means something(characteristic or anything else) passed down from the parent to the children. In the same way in the c++ language, derive the class from the other class(base class) is known as inheritance. The base class is known as parent class and the drive class is known as child class . There are various models of the inheritance  Types of inheritance: we are discussing models of inheritance in cpp programming. Multilevel inheritance  Multiple inheritances Hierarchical inheritance Multilevel inheritance : In this c++ language, derived class from the base but again derived from the derived class.This form of inheritance known as multilevel inheritance. class A { ... .. }; class B:public A { ... .. }; class C: public B { ... .. }; Example of multilevel inheritance: #include <iostream> using namespace std; class classA //create base  {     public:       void display()    

C++ inheritance

Image
  C++ inheritance The most important concept in object-oriented programming is inheritance. G enerally, Inheritance means something(characteristic or anything else) passed down from the parent to the children. In the same way in the c++ language, derive from the other class(base ) is known as inheritance. The base class is known as parent class and the drived is known as child class . The derived class has all the features of the base(parent) class and also has its own features. Why inheritance used in c++? Let’s take an example to understand the concept of inheritance. Suppose three characters in your project gunman, swordman, and fighterman.you will create three class of each member. Notice that all the characters have common characteristics like speak, eat, age, and sleep. If we make one common class of the common characteristics of all three characters then work will be easier . Otherwise, write code differently for all three characters. It is time-consuming and memory consuming.

C++ Destructor

Image
  Destructor c++ In this article, you will learn about the destructor in the cpp. why destructor and how to use the destructor. What is a destructor? Destructor name of destructor tells that something destroys, Exactly destructor destroy means delete (clean up)the memory in the c++ language. On the other way, the destructor is contrary (opposite) to the constructor. The constructor is used to clearing the object and the destructor is used to remove the object. Why need of destructor in c++? Suppose we use heap memory or main memory and we allocate some memory but we do not free the memory for computers.Then there is memory wastage problem happened and problem in memory management. There is a need for something which clears the memory then destructor come into being. Destructor just frees the memory space. How to use destructor? The syntax of destructor in c++:          

Static members in class C++

Image
C++  Static members in a class  Why static members used in classes? The static members in class used to declare having members that value do not change when a new object created. For example, we need to count how many objects create then a static member used or needed information about the previous object. How can used static members in classes c++? We can used static keyword to define static members of the class.A static member of the class shared by all the objects of the class. During the initialization of the static member is initialized by zero.  Take an example to understand the concept of the static members in classes c++. For example: #include<iostream> using namespace std; class myclass //create a class { private: int number ; char character ; float decimal ; public: static int obj ; myclass(int num,float deci ,char cha ) { decimal=deci; character = cha; number = num ; obj++; //increment

C++ friend function

Image
C++ Friend Function In this article ,we will learn about the friend function,how to declare, define, and why we need friend function. The function of access to all the private and protected data of their classes is called friend function. Why a friend function is needed? In the cpp programming, classes introduce for hiding the data .we can not access the data  Of the private part of the other class. The programmer writes complex code to use the different class data for the program.C++ to introduce the friend function to solve this problem. By creating just friend function How to use the friend function? Following is the syntax of the friend function. Important points during using of the friend function. Declaration of the friend function in c++ Friend keyword is used during the declaration of the friend function. Like a normal function, the friend function also declares inside the class may be private or public part of the class. Defining the friend function in c++ During the defining