Found 27759 Articles for Server Side Programming

C++ Program to Implement Sparse Matrix

Ankith Reddy
Updated on 24-Jun-2020 11:30:57

7K+ Views

A sparse matrix is a matrix in which majority of the elements are 0. An example for this is given as follows.The matrix given below contains 5 zeroes. Since the number of zeroes is more than half the elements of the matrix, it is a sparse matrix.5 0 0 3 0 1 0 0 9A program to implement a sparse matrix is as follows.Example Live Demo#include using namespace std; int main () {    int a[10][10] = { {0, 0, 9} , {5, 0, 8} , {7, 0, 0} };    int i, j, count = 0;    int row = 3, col = 3;    for (i = 0; i < row; ++i) {       for (j = 0; j < col; ++j){          if (a[i][j] == 0)          count++;       }    }    cout

C++ Program to Implement Sorted Array

Arjun Thakur
Updated on 14-Sep-2023 01:46:43

25K+ Views

A sorted array is an array in which each of the elements are sorted in some order such as numerical, alphabetical etc. There are many algorithms to sort a numerical array such as bubble sort, insertion sort, selection sort, merge sort, quick sort, heap sort, etc. More details about sorting the array using selection sort are given below.The selection sort is a sorting method that yields a sorted array. It does so by repeatedly finding the smallest element in the array and interchanging it with the element at the starting of the unsorted part.A program that implements a sorted array ... Read More

Binary Search in C++

Chandu yadav
Updated on 28-Jun-2021 05:55:17

17K+ Views

Binary Search is a method to find the required element in a sorted array by repeatedly halving the array and searching in the half.This method is done by starting with the whole array. Then it is halved. If the required data value is greater than the element at the middle of the array, then the upper half of the array is considered. Otherwise, the lower half is considered. This is done continuously until either the required data value is obtained or the remaining array is empty.A program that demonstrates binary search in C++ is given below.Example Live Demo#include using namespace std; ... Read More

Access Modifiers in C++

George John
Updated on 24-Jun-2020 11:34:49

1K+ Views

Access Modifiers are used to implement data hiding in object oriented programming. There are three types of access modifiers used in C++. These are public, private and protected. Details about these are given as follows.Public Access ModifierThe data members and member functions in a class that are declared public are available to everyone, including other classes. They can be accessed from any place in the program using the dot operator with the class object.A program that demonstrates public access specifier is given as follows.Example Live Demo#include using namespace std; class Add {    public:    int a, b;    void sum() ... Read More

Default Constructors in C++

Ankith Reddy
Updated on 24-Jun-2020 11:35:35

15K+ Views

Constructors are functions of a class that are executed when new objects of the class are created. The constructors have the same name as the class and no return type, not even void. They are primarily useful for providing initial values for variables of the class. The two main types of constructors are default constructors and parameterized constructors.Default constructors do not take any parameters. If a default constructor is not provided by the programmer explicitly, then the compiler provides a implicit default constructor. In that case, the default values of the variables are 0.A program that demonstrates default constructors is ... Read More

Destructors in C++

Arjun Thakur
Updated on 24-Jun-2020 11:36:56

16K+ Views

Destructors in C++ are members functions in a class that delete an object. They are called when the class object goes out of scope such as when the function ends, the program ends, a delete variable is called etc.Destructors are different from normal member functions as they don’t take any argument and don’t return anything. Also, destructors have the same name as their class and their name is preceded by a tilde(~).A program that demonstrates destructors in C++ is given as follows.Example Live Demo#include using namespace std; class Demo {    private:    int num1, num2;    public:    Demo(int n1, ... Read More

Copy Constructor in C++

Chandu yadav
Updated on 24-Jun-2020 11:38:16

14K+ Views

The copy constructor is a type of constructor. It creates an object and initializes it with an object of the same class. If the copy constructor is not defined in the class, the compiler itself defines one. A copy constructor is a must for a class that has pointer variables or dynamic memory allocations.A program that demonstrates a copy constructor is as follows.Example Live Demo#include using namespace std; class Demo {    private:    int num1, num2;    public:    Demo(int n1, int n2) {       num1 = n1;       num2 = n2;    }    Demo(const ... Read More

Constructors in C++

Arjun Thakur
Updated on 24-Jun-2020 11:39:55

8K+ Views

Constructors are functions of a class that are executed when new objects of the class are created. The constructors have the same name as the class and no return type, not even void. They are primarily useful for providing initial values for variables of the class.The two main types of constructors are default constructors and parameterized constructors. Details about these are given as follows.Default ConstructorsDefault constructors do not take any parameters. If a default constructor is not provided by the programmer explicitly, then the compiler provides a implicit default constructor. In that case, the default values of the variables are ... Read More

Nested Classes in C++

Ankith Reddy
Updated on 24-Jun-2020 11:40:43

22K+ Views

A nested class is a class that is declared in another class. The nested class is also a member variable of the enclosing class and has the same access rights as the other members. However, the member functions of the enclosing class have no special access to the members of a nested class.A program that demonstrates nested classes in C++ is as follows.Example Live Demo#include using namespace std; class A {    public:    class B {       private:       int num;       public:       void getdata(int n) {          num = n;       }       void putdata() {          cout

Local Class in C++

George John
Updated on 24-Jun-2020 11:23:17

2K+ Views

A class declared inside a function is known as a local class in C++ as it is local to that function.An example of a local class is given as follows.#include using namespace std; void func() {    class LocalClass {    }; } int main() {    return 0; }In the above example, func() is a function and class LocalClass is defined inside the function. So, it is known as a local class.A local class name can only be used in its function and not outside it. Also, the methods of a local class must be defined inside it only. ... Read More

Advertisements