Posts

Showing posts with the label Cpp Programming

Operator Overloading Types and Rules | C++ Programming

Operator Overloading It is the mechanism of giving special meanings to an operator. By overloading operators, we can give additional meanings to operators like +, *, -, <=, >=, etc. which by default are supposed to work only on standard data types like ints, and floats. In general, a = b + c; works only with basic types like ‘int’ and ‘float’, and attempting to apply it when a, b and c are objects of a user-defined class will cause complaints from the compiler. But, using overloading, we can make this statement legal even when a, b and c are user-defined types (objects). Even though the semantics of an operator can be expressed, we cannot change its syntax. When an operator is overloaded, its original meaning is not lost. The grammar rules defined by C++ that govern its use such as the number of operands, precedence, and associatively of the operator remain the same for overloaded operators.  We can overload (give additional meanings to) all the C++ operators except the follo

Friend Functions | C++ Programming

Image
Friend Functions Friend Function A function is said to be a friend function of a class if it can access the members (including private members) of the class even if it is not the member function of this class. In other words, a friend function is a non-member function that has access to the private members of the class. Characteristics of a friend function: A friend function can be either global or a member of some other class. Since the friend function is not a part of the class, it can be declared anywhere in the public, private, and protected sections of the class. It cannot be called by using the object of the class since it is not in the scope of the class. It is called a normal function without the help of any object. Unlike member functions, it cannot access member names directly. So, it has to use an object name and dot operator with each member name (like A.x) It, generally, takes objects as arguments. The concepts of data hiding and encapsulation dictate that private

C++ Program to Represent the Graph Using Adjacency Matrix | C++ Programming

Image
 C++ Program to Represent the Graph Using Adjacency Matrix #include <iostream> #include <iomanip> using namespace std ; // A function to print the adjacency matrix. void PrintMat ( int mat [][ 20 ], int n ) {     int i , j ;     cout << " \n\n "          << setw ( 4 ) << "" ;     for ( i = 0 ; i < n ; i ++)         cout << setw ( 3 ) << "(" << i + 1 << ")" ;     cout << " \n\n " ;     // Print 1 if the corresponding vertexes are connected otherwise 0.     for ( i = 0 ; i < n ; i ++)     {         cout << setw ( 3 ) << "(" << i + 1 << ")" ;         for ( j = 0 ; j < n ; j ++)         {             cout << setw ( 4 ) << mat [ i ][ j ];         }         cout << " \n\n " ;     } } int main () {     int i , j , v ;     cout << "Enter the number of vertexes: " ;

C++ Program for modular search and linear search using recursion function | C++ Programming

C++ Program for modular search and linear search using recursion function Before starting with the program to find binary search and linear search using recursion function let us know about recursion function and Modular Search. Recursion Function Recursion is a process by which a function call itself repeatedly until some specified condition has been satisfied.                  The process is used for repetitive computation in which action is stated in terms of previous result.                  In order to solve a problem recursively two condition must be satisfied. The problem must be written in a recursive form. The problem statement must include a problem stopping condition.

Difference Between Procedure Oriented Programming (POP) & Object Oriented Programming (OOP)

Difference Between Procedure Oriented Programming (POP) & Object-Oriented Programming (OOP) | C++ Programming Procedure Oriented Programming Object-Oriented Programming Divided Into In POP, the program is divided into small parts called  functions . In OOP, the program is divided into parts called  objects . Importance In POP, Importance is not given to  data  but to functions as well as the  sequence  of actions to be done. In OOP, Importance is given to the data rather than procedures or functions because it works as a  real world . Approach POP follows  Top-Down approach . OOP follows  Bottom-Up approach . Access Specifiers POP does not have any access specifier. OOP has access specifiers named Public, Private, Protected, etc. Data Moving In POP, Data can move freely from function to function in the system. In OOP, objects can move and communicate with each other through member functions. Expansion To add new data and function in POP is not so easy. OOP provides an

C++ Program For Banking System Using Class | C++ Programming

C++ Program For Banking System Using Class #include <iostream> using namespace std ; #include <iomanip> class bank {     char name [ 20 ];     int acno ;     char actype [ 20 ];     int bal ; public:     void opbal ( void );     void deposit ( void );     void withdraw ( void );     void display ( void ); }; void bank :: opbal ( void ) {     cout << endl          << endl;     cout << "Enter Name :-" ;     cin >> name ;     cout << "Enter A/c no. :-" ;     cin >> acno ;     cout << "Enter A/c Type :-" ;     cin >> actype ;     cout << "Enter Opening Balance:-" ;     cin >> bal ; } void bank :: deposit ( void ) {     cout << "Enter Deposit amount :-" ;     int deposit = 0 ;     cin >> deposit ;     cout << " \n Deposit Balance = " << bal + deposit ; } void bank :: withdraw ( void ) {     int withdraw ;     c

C++ Program for Constructor Overloading | C++ Programming

Constructor Overloading When more than one constructor function is defined in a class, then it is called constructor overloading or the use of multiple  constructors  in a class. It is used to increase the flexibility of a class by having a greater number of constructors for a single class. Overloading constructors in C++ programming gives us more than one way to initialize objects in a class.

C++ Program To Find Negation Of A Number | C++ Programming

C++ Program To Find Negation Of A Number #include <iostream> using namespace std ; class point {     int x , y ; public:     void getdata ()     {         cout << "Enter x and y coordinate:" ;         cin >> x >> y ;     }     void display ()     {         cout << "(" << x << "," << y << ")" ;     }     point operator- ()     {         point t ;         t . x = - x ;         t . y = - y ;         return t ;     } }; int main () {     point p , q ;     p . getdata ();     q = - p ;     cout << "q=" ;     q . display (); } OUTPUT Enter x and y coordinate: 5 4 q=(-5,-4)

Access Modifiers in C++

Access Modifiers in C++ Access modifiers are constructs that define the scope and visibility of members of a class. There are three access modifiers:

Types of Inheritance in C++ | C++ Programming

Types of Inheritance A class can inherit properties from one or more classes and from one or more levels. On the basis of this concept, there are five types of inheritance. Single Inheritance Multiple Inheritance Hierarchical Inheritance Multilevel Inheritance Hybrid Inheritance Single Inheritance In single inheritance, a class is derived from only one base class. The example and figure below show this inheritance. Example class A {  members of A }; class B  :  public A {  members of B }; Multiple Inheritance In this inheritance, a class is derived from more than one base class. The example and figure below show this inheritance. Implementation Skeleton: class A { members of A }; class B {  members of B }; class C  : public A, public B  { members of C }; Hierarchical Inheritance In this type, two or more classes inherit the properties of one base class. The example and figure below show this inheritance. Implementation Skeleton: class A { member

C++ Program To Find Factorial Of Two Numbers Using Recursion Function | C++ Programming

C++ Program To Find Factorial Of Two Numbers Using Recursion Function #include<iostream> using namespace std; long int fact(int); int main() { int n1,n2; cout<<"Enter number"< cin>>n1>>n2; cout<<n1<<"! = "<<fact(n1)<<endl; cout<<n2<<"! = "<<fact(n2)<<endl; } long int fact(int n) { if(n<=1) return 1; else   return (n*fact(n-1)); } OUTPUT Enter number 5 4 5! = 120 4! = 24

Type Conversion In C++

Type Conversion The type conversions are automatic as long as the data types involved are built-in types. If the data types are user-defined, the compiler does not support automatic type conversion and therefore, we must design the conversion routines by ourselves. Three types of situations might arise in the data conversion in this case. 1. Conversion from basic type to class type 2. Conversion from class type to basic type 3. Conversion from one class type to another class type

C++ Program To Multiply Matrices | C++ Programming

C++ Program To Multiply Matrices #include<iostream> using namespace std; class Matrix { private: int a[10][10]; public: void read_matrix(int m, int n); void display_matrix(int m, int n); void multiply_matrix(int ,int, int,Matrix x, Matrix y); }; void Matrix::read_matrix(int m,int n) { for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { cin>>a[i][j]; } } } void Matrix::display_matrix(int m,int n) { for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { cout<<a[i][j] <<"\t"; } cout<<endl; } } void Matrix::multiply_matrix(int r1,int c1, int c2, Matrix x, Matrix y) {  for(int i=0; i<r1; i++)     { for(int j=0; j<c2; j++)         {         a[i][j]=0; for(int k=0;k<c1;k++)         {         a[i][j]+=x.a[i][k]*y.a[k][j]; }  } } } int main() { int r1,c1,r2,c2; cout<<"Enter order of first matrix: "; cin>>r1>>c1; cout<<"Enter order of seond matrix: "; cin>>r2>>c2; Matrix mat1,

C++ Program To Calculate Tax Using Constructor and Destructor | C++ Programming

C++ Program To Calculate Tax Using Constructor and Destructor  Calculating the tax using the C++ program is easy if you have the concept of constructor and destructor. There is also another way to calculate Tax using C++ which is simple but the use of a constructor is much more practical in the programming world. Let us have some knowledge about the class constructor and destructor below.