Customizable training materials to learn C++

Nano teach C++ found result for your search here:

C++ Keywords

The C + + Keywords must be in your information because you can not use them as variable name.

C++ Keywords List

You can’t use below mentioned keyword as identifier in your C++ programs.
asm else new this
auto enum operator throw
bool explicit private true
break export protected try
case extern public typedef
catch false register typeid
char float reinterpret_cast typename
class for return union
const friend short unsigned
const_cast goto signed using
continue if sizeof virtual
default inline static void
delete int static_cast volatile
do long struct wchar_t
double mutable switch while
dynamic_cast namespace template

In addition, the following words are also reserved

And bitor not_eq xor
and_eq compl or xor_eq
bitand not or_eq
You should also try to avoid using names from the C++ library, e.g. swap, max.
Read More »

C++ Objects and Classes

In object-oriented programming languages like C++, the data and functions (procedures to manipulate the data) are bundled together as a self-contained unit called an object. A class is an extended concept similar to that of structure in C programming language; this class describes the data properties alone. In C++ programming language, class describes both the properties (data) and behaviors (functions) of objects. Classes are not objects, but they are used to instantiate objects.

What is a class ?

  • A class is an abstract data type similar to ‘C‘ structure.
  • Class representation of objects and the sets of operations that can be applied to such objects.
  • Class consists of Data members and methods.
Primary purpose of a class is to held data/information. This is achieved with
attributes which is also know as data members.
The member functions determine the behavior of the class i.e. provide
definition for supporting various operations on data held in form of an
object.

Definition of a class

Syntax:
Class class_name
{
    Data Members;
    Methods;
}
Example:
class A
{
    public:
    double length; // Length of a box
    double breadth; // Breadth of a box
    double height; // Height of a box 
}
  • Private, Protected, Public are called visibility labels.
  • The members that are declared private can be accessed only from within the class.
  • Public members can be accessed from outside the class also.
  • In C++, data can be hidden by making it private.
  • Encapsulation is thus achieved.

Class Members

Data and functions are members.
Data Members and methods must be declared within the class definition.
Example:
Class A
{
    int i; // i is a data member of class A
    int j; // j is a data member of class A
    int i; // Error redefinition of i 
}
  • A member cannot be redeclare within a class.
  • No member can be added elsewhere other than in the class definition.
Example:
Class A
{
    int i;
    int j;
    void f (int, int);
    int g(); 
}
f‘ and ‘g‘ are member function of class A. They determine the behavior of the objects of the class A.

Accessing the Data Members

The public data members of objects of a class can be accessed using the direct member access operator (.). Let us try following example to make the things clear:
Example:
#include <iostream> 
using namespace std;

class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
}

int main( )
{
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here

// box 1 specification
Box1.height = 4.0; 
Box1.length = 6.0; 
Box1.breadth = 3.0;

// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 12.0;

// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl; 

// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}
Program Output:
Volume of Box1 : 72
Volume of Box2 : 1440
It is important to note that private and protected members can not be accessed directly using direct member access operator (.). We will learn how private and protected members can be accessed.

Program to enter students details and display it

Example:
#include<iostream>
using namespace std;

class stud 
{
    public:
    char name[30],clas[10];
    int rol,age;
    float per;

void enter() 
{
    cout<<"\n Enter Student Name, Age, Roll number";     cin>>name>>age>>rol;
    cout<<"\n Enter Student Class and percentage in previous class";     cin>>clas>>per; 
}

void display() 
{
    cout<<"\n Age\tName\tR.No.\tClass\t%ge";
    cout<<"\n"<<age<<"\t"<<name<<"\t"<<rol<<"\t"<<clas<<"\t"<<per<<"%"; 
}
}

int main()
{
    class stud s;
    s.enter();
    s.display();
    cin.get(); //use this to wait for a keypress 
} 
Program Output:

Read More »

C++ Inheritance

One of the most important concepts in object-oriented programming is that of inheritance. Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationships between them and 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, 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.

Forms of Inheritance

  • Single Inheritance – If a class is derived from a single base class, it is called as single inheritance.
  • Multiple Inheritance – If a class is derived from more than one base class, it is known as multiple inheritance.
  • Multilevel Inheritance – The classes can also be derived from the classes that are already derived. This type of inheritance is called multilevel inheritance.
  • Hierarchical Inheritance – If a number of classes are derived from a single base class, it is called as hierarchical inheritance.
  • Hybrid Inheritance – This is a Mixture of two or More Inheritance and in this Inheritance a Code May Contains two or Three types of inheritance in Single Code.

Visibility Mode

It is the keyword that controls the visibility and availability of inherited base class members in the derived class. It can be either private or protected or public.
  • Private Inheritance – When deriving from a private base class, public and protected members of the base class become private members of the derived class.
  • Public Inheritance – When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class’s private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.
  • Protected Inheritance – When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.
Base class visibility Derived class visibility
Public derivation Private derivation Protected derivation
Private Not inherited Not inherited Not inherited
Protected Protected Private Protected
Public Public Private Protected
Read More »

C++ Friend Function

In C++ a function or an entire class may be declared to be a friend of another class or function. Friend function can also be used for function overloading.
Friend function declaration can appear anywhere in the class. But a good practice would be where the class ends. An ordinary function that is not the member function of a class has no privilege to access the private data members, but the friend function does have the capability to access any private data members. The declaration of the friend function is very simple. The keyword friend in the class prototype inside the class definition precedes it.

Example to Demonstrate working of friend Function

/* C++ program to demonstrate the working of friend function.*/
#include <iostream>
using namespace std; 

class Distance {
    private:
        int meter; 
    public:
        Distance(): meter(0){ }
        friend int func(Distance); //friend function 
};

int func(Distance d){
    //function definition
    d.meter=10; //accessing private data from non-member function
    return d.meter; 
}

int main(){ Distance D;
    cout<<"Distace: "<<func(D);
    system("pause"); 
    return 0;
}
Program Output:
Distance: 10
Here, friend function func() is declared inside Distance class. So, the private data can be accessed from this function. Though this example gives you what idea about the concept of friend function.
In C++, friend means to give permission to a class or function. The non-member function has to grant an access to update or access the class.
The advantage of encapsulation and data hiding is that a non-member function of the class cannot access a member data of that class. Care must be taken using friend function because it breaks the natural encapsulation, which is one of the advantages of object oriented programming. It is best used in the operator overloading.
Read More »

C++ Loops, Creating a Pyramid

C++ program to create a pyramid using for loop and if condition.
Example:
#include<iostream>
using namespace std;

int main()
{
   int i,j,k,space=10; // to print the pyramid in center, you can also increase the # of spaces
   
for (int i=0;i<=5;i++)
{
   for (int k=0;k<space;k++)
{
   cout<<" ";
}
for (int j=0;j<2*i-1;j++)
{
   cout<<"*";
}
   space--;
   cout<<endl;
}
cin.get(); /*use this to wait for a keypress*/

} 
 















Program Output:
         *
        ***
       *****
      *******
     *********
Read More »

C++ Program to find Perfect Number

Perfect number is a positive number which sum of all positive divisors excluding that number.
For example: 6 is Perfect Number since divisor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6
and 28 is also a Perfect Number
since 1+ 2 + 4 + 7 + 14= 28
Other perfect numbers: 496, 8128
Example:
#include<iostream> 
#include<cctype>
using namespace std; 

int main(){
    int n,i=1,sum=0;
    cout << "Enter a number: ";
    cin >> n;
       while(i<n){
       if(n%i==0)
       sum=sum+i;
       i++; 
}
 
if(sum==n)
    cout << i << " is a perfect number\n"; 
else
    cout << i << " is not a perfect number\n";
    system("pause"); 

return 0;

}
Program Output:
6 is a perfect number
15 is not a perfect number
28 is a perfect number
496 is a perfect number
Read More »

C++ Compute the Sum and Average of Two Numbers

This program takes in two integers x and y as a screen input from the user.
The sum and average of these two integers are calculated and outputted using the ‘cout’ command.
Example:
#include<iostream>
using namespace std; 

int main(){ 

    int x,y,sum;
    float average;
    cout << "Enter 2 integers : " << endl;
    cin>>x>>y;
    sum=x+y;
    average=sum/2;
    cout << "The sum of " << x << " and " << y << " is " << sum << "." << endl;
    cout << "The average of " << x << " and " << y << " is " << average << "." << endl;
    system("pause");

}
Program Output:
Enter 2 integers :
12
15
The sum of 12 and 15 is 27.
The average of 12 and 15 is 13.
Read More »