Posts

Showing posts from September, 2017

C ++ Arrays

Image
C++ Arrays In this article, you will learn to work with arrays. You will learn to declare, initialize and, access array elements in C++ programming. In programming, one of the frequently arising problem is to handle numerous data of same type. Consider this situation, you are taking a survey of 100 people and you have to store their age. To solve this problem in C++, you can create an integer array having 100 elements. An array is a collection of data that holds fixed number of values of same type. For example: int age[100]; Accessing the values of an array The values of any of the elements in an array can be accessed just like the value of a regular variable of the same type. The syntax is: Following the previous examples in which foo had 5 elements and each of those elements was of type int, the name which can be used to refer to each element is the following: Example #include <iostream > using namespace std; int main() { int numbers[5], sum = 0; co...

Abstract classes

                                      Abstract classes abstract class is a class that is designed to be specifically used as a base class. abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration class Box { public: // pure virtual function virtual double getVolume() = 0; private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; Abstract Class Example Consider the following example where parent class provides an interface to the base class to implement a function called getArea() − #include <iostream.h> using namespace std; // Base class class Shape { public: // pure virtual function providing interface ...

Cpp Factorial of a given number

                                            Factorial of a given number Factorial of negative number cannot be found and factorial of 0 is 1. In this program below, user is asked to enter a positive integer. Then the factorial of that number is computed and displayed in the screen. Example Factorial of a given number #include using namespace std; int main() { unsigned int n; unsigned long long factorial = 1; cout << "Enter a positive integer: "; cin >> n; for(int i = 1; i <=n; ++i) { factorial *= i; } cout << "Factorial of " << n << " = " << factorial; return 0; } Output: Enter a positive integer: 12 Factorial of 12 = 479001600 << PREVIEW >> << NEXT >>

Cpp Example Prime Number Check

                                              Example Prime Number Check A positive integer which is only divisible by 1 and itself is known as prime number. For example: 13 is a prime number because it is only divisible by 1 and 13 but, 15 is not prime number because it is divisible by 1, 3, 5 and 15. Example: Check Prime Number #include using namespace std; int main() { int n, i; bool isPrime = true; cout << "Enter a positive integer: "; cin >> n; for(i = 2; i <= n / 2; ++i) { if(n % i == 0) { isPrime = false; break; } } if (isPrime) cout << "This is a prime number"; else cout << "This is not a prime number"; return 0; } Output Enter a positive integer: 29 This is a prime number. << PREVIEW >> ...
Image
Types Of Inheritance One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, Types Of Inheritance 1.Single Inheritance 2.Multiple Inheritance 3.Hierarchical Inheritance 4.Multilevel Inheritance 5.Hybrid Inheritance (also known as Virtual Inheritance) 1. Single Inheritance In this type of inheritance one derived class inherits from only one base class. It is the most simplest form of Inheritance. Syntax : class base_classname { properties; methods; }; class derived_classname : visibility_mode base_classname { properties; methods; }; Example : #include <iostream.h> #include <conio.h> using namespace std; class person /*Parent class*/ { private: char fname[100],lname[100],gender[10]; protected: int age; public: void input_person(); void display_person(); }; class student: public ...

Inheritance in cpp

Inheritance in cpp One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. Defination Of Inheritance Derive quality and characteristics from parents or ancestors. Like you inherit features of your parents. Syntax class subclass_name : access_mode base_class_name { //body of subclass }; Example #include using namespace std; //Base class class Parent { public: int id_p; }; // Sub class inheriting from ...

Cpp Classes

Cpp Classes A class in C++ is a user defined type or data structure declared with keyword class that has data and functions (also called methods) as its members whose access is governed by the three access specifiers private protected or public (by default access to members of a class is private). The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class. outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class. Instances of a class data type are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer. Definition Of Class The C++ programming language allows programmers to separate program-specific datatypes through the use of classes. Classes defi...
History Of Cpp In 1979, Bjarne Stroustrup, a Danish computer scientist, began work on "C with Classes", the predecessor to C++.[8] The motivation for creating a new language originated from Stroustrup's experience in programming for his Ph.D. thesis. Stroustrup found that Simula had features that were very helpful for large software development, but the language was too slow for practical use, while BCPL was fast but too low-level to be suitable for large software development. When Stroustrup started working in AT&T Bell Labs, he had the problem of analyzing the UNIX kernel with respect to distributed computing. Remembering his Ph.D. experience, Stroustrup set out to enhance the C language with Simula-like features.[9] C was chosen because it was general-purpose, fast, portable and widely used. As well as C and Simula's influences, other languages also influenced C++, including ALGOL 68, Ada, CLU and ML. Initially, Stroustrup's "C with Class...