Skip to main content

Posts

Showing posts with the label Advance C

C++: 9. File Handling

Files are used to store data in a storage device permanently. File handling provides a mechanism to store the output of a program in a file and perform various operations on it. Example of opening/creating a file    #include<iostream> #include<fstream> using namespace std; int main() { fstream new_file; new_file.open("new_file",ios::out); if(!new_file) { cout<<"File creation failed"; } else { cout<<"New file created"; new_file.close(); // Step 4: Closing file } return 0; } Writing to a File #include <iostream> #include <fstream> using namespace std; int main() { fstream new_file; new_file.open("new_file_write.txt",ios::out); if(!new_file) { cout<<"File creation failed"; } else { cout<<"New file created"; new_file<<"Learning File handling"; //Writing to file new_file.close();

C++: 8. Polymorphism

Polymorphism is an important concept of object-oriented programming. It simply means more than one form. That is, the same entity (function or operator) behaves differently in different scenarios. Function Overloading in C++ In C++, two or more functions can have the same name if the number and/or type of parameters are different, this is called function overloading. Example:-  #include <iostream> using namespace std; int add(int a, int b) {     return a+b; } int add(int a, int b, int c) {     return a+b+c; } double add(double a, double b) {     return a+b; } int main() {   int x = 3, y = 7, z = 12;   double n1 = 4.56, n2 = 13.479;   cout<<"x+y = "<<add(x,y)<<endl;   cout<<"x+y+z = "<<add(x,y,z)<<endl;   cout<<"n1+n2 = "<<add(n1,n2);   return 0; } Output:-  x+y = 10 x+y+z = 22 n1+n2 = 18.039 Function Overriding in C++ When a member function of a base class is redefined in its derived class with the same

C++: 7. Inheritance

In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class. In C++, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class. Single Level Inheritance When one class inherits another class, it is known as single level inheritance. Example:-  #include <iostream>   using namespace std;    class Account {      public:      float salary = 60000;     };      class Programmer: public Account {      public:      float bonus = 5000;        };        int main(void) {        Programmer p1;        cout<<"Salary: "<<p1.salary<<endl;          cout<<"Bonus: "<<p1.bonus<<endl;         return 0;   }   Output:-  Salary: 6

C++: 6. Constructors

A constructor is a special type of member function that is called automatically when an object is created. In C++, a constructor has the same name as that of the class and it does not have a return type. C++ Default Constructor A constructor which has no argument is known as default constructor. It is invoked at the time of creating object. Example:-  #include <iostream>   using namespace std;   class Employee    {      public:           Employee()             {                 cout<<"Default Constructor Invoked"<<endl;             }     };   int main(void)    {       Employee e1; //creating an object of Employee        Employee e2;        return 0;   }   Output:-  Default Constructor Invoked  Default Constructor Invoked  C++ Parameterized Constructor A constructor which has parameters is called the parameterized constructor. It is used to provide different values to distinct objects. Example:-  #include <iostream> using namespace std; class Employee {

C++: 5. Friend function

If a function is defined as a friend function in C++, then the protected and private data of a class can be accessed using the function. By using the keyword friend compiler knows the given function is a friend function. For accessing the data, the declaration of a friend function should be done inside the body of a class starting with the keyword friend. Syntax:- class class_name     {         friend data_type function_name(argument/s);            // syntax of friend function.   };     Example:-  #include <iostream>     using namespace std;     class Box     {         private:             int length;         public:             Box(): length(0) { }             friend int printLength(Box); //friend function     };     int printLength(Box b)     {        b.length += 10;         return b.length;     }     int main()     {         Box b;         cout<<"Length of box: "<< printLength(b)<<endl;         return 0;     }     Output:-  Length of box: 10   Chara

C++: 4. Array of Objects

You can store objects of user defined datatype in a C++ Array. To store objects of user defined datatype in an array, you can declare an array of the specific type and initialize the array Advantages of Array of Objects: The array of objects represent storing multiple objects in a single name. In an array of objects, the data can be accessed randomly by using the index number. Reduce the time and memory by storing the data in a single variable. Example:-  // C++ program to implement // the above approach #include<iostream> using namespace std;   class Employee {   int id;   char name[30];   public:       // Declaration of function   void getdata();       // Declaration of function   void putdata(); };   // Defining the function outside // the class void Employee::getdata() {   cout << "Enter Id : ";   cin >> id;   cout << "Enter Name : ";   cin >> name; }   // Defining the function outside // the class void Employee::putdata() {   cout

C++: 3. Scope resolution operator

The scope resolution operator ( :: ) is used for several reasons. For example: If the global variable name is same as local variable name, the scope resolution operator will be used to call the global variable. It is also used to define a function outside the class To access a global variable when there is a local variable with same name: #include <iostream>   using namespace std;   // declare global variable   int num = 50;   int main ()   {   // declare local variable    int num = 100;   // print the value of the variables   cout << " The value of the local variable num: " << num;   // use scope resolution operator (::) to access the global variable    cout << "\n The value of the global variable num: " << ::num;    return 0;   }   Output:-  The value of the local variable num: 100 The value of the global variable num: 50 To define the member function outside of the class: #include <iostream>   using namespace std;   class Opera

C++: 2. OOPs Concepts

; OOP stands for Object-Oriented Programming.  As the name suggests uses objects in programming. Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance.  Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. Class It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. Object Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical. Object-oriented programming has several advantages OOP is faster and easier to execute OOP provides a clear structure for the programs OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code easier to

C++: 1. Introduction

C++ is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell Labs. C++ is a MUST for students and working professionals to become a great Software Engineer. Why to Learn C++ C++ is very close to hardware, so you get a chance to work at a low level which gives you lot of control in terms of memory management, better performance and finally a robust software development. C++ programming gives you a clear understanding about Object Oriented Programming. C++ really teaches you the difference between compiler, linker and loader, different data types, storage classes, variable types their scopes etc. Application of C++ Application Software Development  Programming Languages Development Games Development Computation Programming How to Use Use any one of the link to directly see it in action in your browser:- https://www.programiz.com/cpp-programming/online-compiler/ https://www.tutorialspoint.com/compile_cpp_online.php https://www.onlinegdb.com/online_c++