• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » C++ Programming

Call by Value and Call by Reference

By Dinesh Thakur

1) Call by Value:-when we call a Function and if a function can accept the Arguments from the Called Function, Then we must have to Supply some Arguments to the Function. So that the Arguments those are passed to that function just contains the values from the variables but not an Actual Address of the variable.

So that generally when we call a Function then we will just pass the variables or the Arguments and we doesn’t Pass the Address of Variables , So that the function will never effects on the Values or on the variables. So Call by value is just the Concept in which you must have to Remember that the values those are Passed to the Functions will never effect the Actual Values those are Stored into the variables.

2) Call By Reference :-When a function is called by the reference then the values those are passed in the calling functions are affected when they are passed by Reference Means they change their value when they passed by the References. In the Call by Reference we pass the Address of the variables whose Arguments are also Send. So that when we use the Reference then, we pass the Address the Variables.

When we pass the Address of variables to the Arguments then a Function may effect on the Variables. Means When a Function will Change the Values then the values of Variables gets Automatically Changed. And When a Function performs Some Operation on the Passed values, then this will also effect on the Actual Values.

What is Recursion?

By Dinesh Thakur

When a Function can call itself again and Again or When a Function call itself until a Condition is not to be False. When a function definition includes a call to itself, it is referred to as a recursive function and the process is known as recursion or circular definition. In this a function call itself repeatedly. In the Recursion we just have to make a One Time call and the will automatically call itself Again and Again.

When a recursive function is called for the first time, a space is set aside in the memory to execute this call and the function body is executed. Then a second call to a function is made; again a space is set for this call, and so on. In other words, memory spaces for each function call are arranged in a stack. Each time a function is called; its memory area is placed on the top of the stack and is removed when the execution of the call is completed.

                      Recursive functions

To understand the concept of recursive function, consider this example.

 

Example : A program to demonstrate the concept of recursive function

 

#include<iostream>

using namespace std;

void reverse ();

int main ()

{

reverse () ;

cout<<” is the reverse of the entered characters”;

return 0;

}

void reverse ()

char ch;

cout<<”Enter a character (‘/’ to end program): “;

cin>>ch;

if (ch != ‘/’)

{

reverse() ;

cout<<ch;

}

}

 

The output of the program is

 

Enter a character (‘/’ to end program) : h

Enter a character (‘/’ to end program) : i

Enter a character (‘/’ to end program) : /

ih is the reverse of the entered characters

 

In this example, function reverse ()is called to accept a character from the user. The function reverse ()calls itself again and again until the user enters’ /’ and prints the reverse of the characters entered.

Note that the recursive functions can also be defined iteratively using “for”, “while” and “do …while” loops. This is because recursion makes the program execution slower due to its extra stack manipulation and more memory utilization. In addition, recursion sometimes results in stack overflow, as for each function call, new memory space is allocated to local variables and function parameters on the stack. However, in some cases, recursive functions are preferred over their iterative counterparts as they make code simpler and easier to understand. For example, it is easier to implement Quick sort algorithm using recursion.

What is Inheritance? Types of Inheritance in c++

By Dinesh Thakur

Inheritance means using the Pre-defined Code. Inheritance is one of the key concepts in the Object-Oriented Programming language like C++, enabling you to organize classes in a hierarchical form. Just like a child inherits the characteristics of his parents and add specific new attributes of his own. With the help of Inheritance, we use the previously defined code but always Remembers, We are only using that code but not changing that code. [Read more…] about What is Inheritance? Types of Inheritance in c++

What do you mean by Inheritance

By Dinesh Thakur

Inheritance can be defined as the process whereby an object of a class acquires characteristics from the object of the other class. All the objects of a similar kind are grouped together to form a class. However, sometimes a situation arises when different objects cannot be combined together under a single group as they share only some common characteristics. In this situation, the classes are defined in such a way that the common features are combined to form a generalized class and the specific features are combined to form a specialized class. The specialized class is defined in such a way that in addition to the individual characteristics and functions, it also inherits all the properties and the functions of its generalized class. [Read more…] about What do you mean by Inheritance

Describe Private, Protected and Public – the differences and give examples

By Dinesh Thakur

1: Public, protected and private are three access specifier in C++. Public data members and member functions are accessible outside the class. Protected data members and member functions are only available to derived classes. Private data members and member functions can’t be accessed outside the class. However there is an exception can be using friend classes. Write a function that swaps the values of two integers, using int* as the argument type.

 

void swap(int* a, int*b) {

int t;

t = *a;

*a = *b;

*b = t;

}

 

Ans.2: class Point2D{

int x; int y;

public int color;

protected bool pinned;

public Point2D() : x(0) , y(0) {} //default (no argument) constructor

};

Point2D MyPoint;

You cannot directly access private data members when they are declared (implicitly)

private:

MyPoint.x = 5; // Compiler will issue a compile ERROR

//Nor yoy can see them:

int x_dim = MyPoint.x; // Compiler will issue a compile ERROR

On the other hand, you can assign and read the public data members:

MyPoint.color = 255; // no problem

int col = MyPoint.color; // no problem

With protected data members you can read them but not write them: MyPoint.pinned = true;

// Compiler will issue a compile ERROR

bool isPinned = MyPoint.pinned; // no problem

 

What is Virtual Class and Friend Class

By Dinesh Thakur

Friend classes are used when two or more classes are designed to work together and need access to each other’s implementation in ways that the rest of the world shouldn’t be allowed to have. In other words, they help keep private things private. For instance, it may be desirable for class DatabaseCursor to have more privilege to the internals of class Database than main() has.

What is the difference between an Object and a Class

By Dinesh Thakur

Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.

A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don’t change.

The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed.

An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.

 

What is the difference between Class and Structure

By Dinesh Thakur

Class : Class is a user-defined data type in C++. It can be created to solve a particular kind of problem. After creation the user need not know the specifics of the working of a class. Class is a successor of Structure. By default all the members inside the class are private.

Structure : Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public.

 

Define a Constructor. What it is and how it might be called (2 methods)

By Dinesh Thakur

1: constructor is a member function of the class, with the name of the function being the same as the class name. It also specifies how the object should be initialized.

 

 Ways of calling constructor :

 

1) Implicitly : automatically by complier when an object is created.

2) Calling the constructors explicitly is possible, but it makes the code unverifiable.

 

2: class Point2D{

int x; int y;

public Point2D() : x(0) , y(0) {} //default (no argument) constructor

};

main(){

Point2D MyPoint; // Implicit Constructor call. In order to allocate memory on stack, the

default constructor is implicitly called.

Point2D * pPoint = new Point2D(); // Explicit Constructor call. In order to allocate memory

on HEAP we call the default constructor.

 

What is Object and Classes

By Dinesh Thakur

Objects are the small, self-contained and modular units with a well-defined boundary. An Object is a Real Word Thing Which performs a Specific Task. An object consists of a state (Properties) and behavior (Method). The state of an object is one of the possible conditions that an object can exist in and is represented by its characteristics or attributes or data or Properties of Object. The behavior of an object determines how an object acts or behaves and is represented by the operations that it can perform. In OOP, the attributes of an object are represented by the variables and the operations are represented by the functions.

For example, A Car is an Object which has a Certain Number of Properties Like Color, Model No, Average etc and a Function Known as Run When A User gives Some Race then the Car Will be Moved. An object biscuit may consist of data product code P00l, product name Britannia Biscuits, price 20 and quantity in hand 50. These data specify the attributes or features of the object. Similarly, consider another object Maggi with data product code P002, product name Maggi Noodles, price 10, and quantity in hand 20. In addition, the data in the object can be used by the functions such as check_qty() and display_product(). These functions specify the actions that can be performed on data.

 

Objects are what actually runs in the computer and thus, are the basic run-time entities in object-oriented systems. They are the building blocks of object-oriented programming. Although, two or more objects can have same attributes, still they are separate and independent objects with their own identity. In other words, all the objects in a system take a separate space in the memory independent of each other. The main objective of breaking down complex software projects into objects is that changes made to one part of a software should not adversely affect the other parts.

                                          Objects

Class : A class is defined as a user-defined data type, which contains the entire set of (properties and Methods) similar data and the functions that the objects possess. In other words, a class in OOP represents a group of similar objects. Like Animals is name of class which contains all the Properties and Methods of an Object of another Animals So if we wants to access any data from the Class then first we have to create the Object of a class Then we can use any data and method from class However, each of them can be categorized under different groups depending on the common properties they possess and the functions they perform. For example, cars, scooters, motorbikes, buses all can be grouped under the category vehicles. Similarly, dogs, cats, horses, etc., can be categorized under the group animals. Thus, vehicles -and animals can be considered as the classes.

 

A class serves as a blueprint or template for its objects. That is, once a class has been defied, any number of objects belonging to that class can be created. The objects of a class are also known as the instances or the variables of that class and the process of creating objects from a class is known as instantiation. Note that a class does not represent an object, rather it represents the data and functions that an object should have.

 

For example, a class Product consists of data such as p_code, p_name, p_price and qty_in_hand which specify the attributes or features of the objects of the Product class. In addition, it consists of functions such as display_product() and check_qty() that specify the actions that can be performed on data.

 

Note that the data belonging to a particular class is known as its data members and the functions of the class are known as the member functions and both collectively are known as the members of the class.

                                 Classes and its Object

What is Overloading. Type of Overloading

By Dinesh Thakur

Overloading: When a single Object has multiple behaviors. Then it is called as Overloading. Overloading is that in which a Single Object has a same name and Provides Many Functions. In Overloading followings things denotes Overloading:-

1)      When an Object has Same Name.
2)      Difference is Return type.
3)      Difference in Function, with Multiple Arguments.
4)      Difference in Data Type.

1)   Constructor Overloading: Constructor overloading is that in which a Constructor has a same name and has multiple Functions, then it is called as Constructor Overloading. As we Know that Constructor are of Default, Parameterized and Copy Constructors. So that when we are creating a Single Constructor with Multiple Arguments then it is called as Constructor Overloading.
2)   Operator Overloading: As we know that Operators are used for Performing Operations on the Operands. But Each and Every Operator has Some Limitations Means an Operator which is also called as Binary are used for Performing Operations on the two Operands and Unary Operators performs their Operation on the single Operand.
So with the help of Operator Overloading, we can Change the Operation of the Operator. Means With the help of Operators we can Change the Operation of the Operators. For Example with the help of Binary Operators we can add two Objects Means not only the two Data Members of the Class, This will add all the Data Members of the Class.
So Like this Way we can Also Change the Behavior of the Unary Operator Means Unary Operators are used for Performing the Operation on the Single Operand. But With the help of Operator Overloading we can Change the behavior of the unary Operator means we can perform Operations means we can Increase or Decrease the values of two or more Operands at a Time.
And With the Help of Comparison Operators we can also compare the Two Objects Means all the Data Members of one Object can be compared with the Data Members of the Other Object. Without the help of Operator Overloading this is not possible to compare two Objects. So with the help of Comparison Operators we can compare two Objects.
3) Method Overloading: – Method Overloading is also called as Function Overloading. Overloading Means a Functions  has many Behaviors occurred When in class when a functions has same name but different behaviors A Functions said to be overloaded When :-

Ø    Function has same Name but Different Return Type
Ø    Difference in No of Arguments
Ø    Different Return Type in Arguments

When We Pass a Call for Execution then it will match the Criteria of Function like Number of Arguments and Data types etc.

Overloading: When a single Object has multiple behaviors. Then it is called as Overloading. Overloading is that in which a Single Object has a same name and Provides Many Functions. In Overloading followings things denotes Overloading:-

1)      When an Object has Same Name.

2)      Difference is Return type.

3)      Difference in Function, with Multiple Arguments.

4)      Difference in Data Type.

1)   Constructor Overloading: Constructor overloading is that in which a Constructor has a same name and has multiple Functions, then it is called as Constructor Overloading. As we Know that Constructor are of Default, Parameterized and Copy Constructors. So that when we are creating a Single Constructor with Multiple Arguments then it is called as Constructor Overloading.

2)   Operator Overloading: As we know that Operators are used for Performing Operations on the Operands. But Each and Every Operator has Some Limitations Means an Operator which is also called as Binary are used for Performing Operations on the two Operands and Unary Operators performs their Operation on the single Operand.

So with the help of Operator Overloading, we can Change the Operation of the Operator. Means With the help of Operators we can Change the Operation of the Operators. For Example with the help of Binary Operators we can add two Objects Means not only the two Data Members of the Class, This will add all the Data Members of the Class.

So Like this Way we can Also Change the Behavior of the Unary Operator Means Unary Operators are used for Performing the Operation on the Single Operand. But With the help of Operator Overloading we can Change the behavior of the unary Operator means we can perform Operations means we can Increase or Decrease the values of two or more Operands at a Time.

And With the Help of Comparison Operators we can also compare the Two Objects Means all the Data Members of one Object can be compared with the Data Members of the Other Object. Without the help of Operator Overloading this is not possible to compare two Objects. So with the help of Comparison Operators we can compare two Objects.

3) Method Overloading: – Method Overloading is also called as Function Overloading. Overloading Means a Functions  has many Behaviors occurred When in class when a functions has same name but different behaviors A Functions said to be overloaded When :-

Ø    Function has same Name but Different Return Type

Ø    Difference in No of Arguments

Ø    Different Return Type in Arguments

When We Pass a Call for Execution then it will match the Criteria of Function like Number of Arguments and Data types etc.

Early Binding and Late Binding

By Dinesh Thakur

Early Binding:Early Binding Always Occur in the Polymorphism, when we pass the Reference of a sub Class into the Pointer Object of Base Class, then the Member Functions Are never to be Override. When we execute the Program then Compiler knows this thing. This is called as Early Binding. And the Compiler will Execute the Member Functions of Base Class and this will never overrides the Body of the Sub Class Member Function. This is known as the early binding.

Late Binding: In the Late Binding the Compiler never knows About the Code. Means what the Code will do. All the Code is understood at the Time of Execution This is called as Late Binding. As we Know that Late Binding is Performed By using the virtual Functions. Virtual Means Function must be Override. When the Compiler Finds out the Word Virtual at the time of Execution then this will override the Body of the Function of Base Class with the Function of sub Class. When we pass the Reference of the Sub Class into the Pointer Object of the Base Class then the Body of the First Function will be Override by the Sub Class Because the Base Class Member Function is declared with the virtual keyword and the virtual means function must be override.

 

Virtual Destructors

By Dinesh Thakur

In the Inheritance when we Points or Pass the Reference of Second Class Object into the Pointer Object of First Class , then the Pointer object of first class will call only the Constructor of Sub Class But Destructors are not Called With the help of Base Class Pointer Object. Because the Statements or Destructors of Base Class are never Override.

 

So When we wants to call the Destructor of Sub Class With the help of First Class Pointer Object then we use the Virtual Destructors When we use the Virtual Destructors then the Body of the Base Class Destructors will automatically Override.

 

Virtual Base Class in C++

By Dinesh Thakur

It may be possible that an inheritance structure may consist of a class that is derived from two or more base classes, both of which in tum are further derived from a common base class as shown below Figure. This class arrangement involves more than one form of inheritance, namely hierarchical, multiple and multilevel inheritances. [Read more…] about Virtual Base Class in C++

What is Classes Objects

By Dinesh Thakur

We know that C++ is an OOP language that is code of C++ may Contains classes there is a main Method which also Reside in Class.

if any one wants to use any data or member functions from Class then first We have to create an object of that class then with the help of dot operator we can call or use any data member or  member function from the class, C++ allows all OOPS Concepts like Data abstraction ,Inheritance, Polymorphism and Data Encapsulation all these are Achieved only you will Create Code in the Form Classes

 

A Class Contains

 

1) Data Members or   Simply Called As Variables

2) Member Functions or Simply Called as Functions

 

Storage Classes

By Dinesh Thakur

Storage Classes are used to determine in which space the value of variable will Stored. And what will be the value of variable, when we doesn’t Specify the value of variable. And In what Place we can use the value of variable. The Storage Classes Determines the Following things.

1) Variable: Where the Value of variable will be Stored. In the Memory or in the Registers. Because the Value which is Stored in the Registers will fastly Accessed, Generally value of a variables is Stored into the Memory. The Registers are near to the CPU So that value which is given by user recently is Stored into the Registers.
2) Default Value: The Storage Class also determines what will be the value of the variable, if doesn’t Specified by a user. Means default value of the user. The Default value of the variable may either a 0 or a garbage value which is specified by the Computer.
3) Scope: The Storage Classes also determines where we can use the value of the variable Means what is the Scope of the variable.

There are two Types of Variables in C++ Language

1)   Local Variable.
2)   Global or External variables

The Local Variables are those which are declared in the Main function or in the Simple Function. The variables those are declared in the Function will never be accessed by Main Function and also vice versa. So that the Local variables those are declared Within a Block and the variables are accessed in the Block only, not from outside the Function. And not in other Functions.
Global variables: The Global variables are those which are accessed by any Function whether it is a Main Function or whether in any function So that the Global variables are those which are accessed from any Function. And the Functions those are Global Scope Accessed by any Function and in any Function we Can Specify the value of variable.

There are four types of Storage Classes those are as Followings: —

1) Automatic Storage Classes: The Automatic Storage Class Specifies that

Value of Variable Will be Stored into the Memory.

Default value: if we don’t specify the value to the variable, then this will accept the Default value as a Garbage Value.
Local Scope: The variables those are declared as Automatic will have a Local Scope Means we can Access the Value only that Location, where we have declared that variable.

If we don’t Specify Any Class, then this is the default Class and this class always takes an auto Keyword.

Static Storage Class

Value of Variable Will be Stored into the Memory.

Default value: if we don’t specify the value to the variable, then this will accept the Default value as a 0.
Local Scope: The variables those are declared as Static will have a Local Scope Means we can Access the Value only that Location, where we have declared that variable.

Registers Storage Class

Value of Variable Will be Stored into the Registers rather than Memory.

Default value: if we don’t specify the value to the variable, then this will accept the Default value as a Garbage Value.
Local Scope: The variables those are declared as Register have a Local Scope Means we can Access the Value only that Location, where we have declared that variable.

External Storage Class

Value of Variable Will be Stored into the Memory.

Default value: if we don’t specify the value to the variable, then this will accept the Default value as a Garbage Value.
Global Scope: The variables those are declared as External will have a Global Scope Means we can Access the Value in any Location, means we access that variable in the Main Function or in any Function.
If we don’t Specify Any Class, then this is the default Class and this class always takes an auto Keyword.

Storage Classes are used to determine in which space the value of variable will Stored. And what will be the value of variable, when we doesn’t Specify the value of variable. And In what Place we can use the value of variable. The Storage Classes Determines the Following things

1) Variable: Where the Value of variable will be Stored. In the Memory or in the Registers. Because the Value which is Stored in the Registers will fastly Accessed, Generally value of a variables is Stored into the Memory. The Registers are near to the CPU So that value which is given by user recently is Stored into the Registers.

2) Default Value: The Storage Class also determines what will be the value of the variable, if doesn’t Specified by a user. Means default value of the user. The Default value of the variable may either a 0 or a garbage value which is specified by the Computer.

3) Scope: The Storage Classes also determines where we can use the value of the variable Means what is the Scope of the variable.

There are two Types of Variables in C++ Language

1)   Local Variable.

2)   Global or External variables

The Local Variables are those which are declared in the Main function or in the Simple Function. The variables those are declared in the Function will never be accessed by Main Function and also vice versa. So that the Local variables those are declared Within a Block and the variables are accessed in the Block only, not from outside the Function. And not in other Functions.

Global variables: The Global variables are those which are accessed by any Function whether it is a Main Function or whether in any function So that the Global variables are those which are accessed from any Function. And the Functions those are Global Scope Accessed by any Function and in any Function we Can Specify the value of variable.

There are four types of Storage Classes those are as Followings: —

1) Automatic Storage Classes: The Automatic Storage Class Specifies that

Value of Variable Will be Stored into the Memory.
Default value: if we don’t specify the value to the variable, then this will accept the Default value as a Garbage Value.
Local Scope: The variables those are declared as Automatic will have a Local Scope Means we can Access the Value only that Location, where we have declared that variable.
If we don’t Specify Any Class, then this is the default Class and this class always takes an auto Keyword.

Static Storage Class

Value of Variable Will be Stored into the Memory.
Default value: if we don’t specify the value to the variable, then this will accept the Default value as a 0.
Local Scope: The variables those are declared as Static will have a Local Scope Means we can Access the Value only that Location, where we have declared that variable.

Registers Storage Class

Value of Variable Will be Stored into the Registers rather than Memory.
Default value: if we don’t specify the value to the variable, then this will accept the Default value as a Garbage Value.
Local Scope: The variables those are declared as Register have a Local Scope Means we can Access the Value only that Location, where we have declared that variable.

External Storage Class

Value of Variable Will be Stored into the Memory.
Default value: if we don’t specify the value to the variable, then this will accept the Default value as a Garbage Value.
Global Scope: The variables those are declared as External will have a Global Scope Means we can Access the Value in any Location, means we access that variable in the Main Function or in any Function.
If we don’t Specify Any Class, then this is the default Class and this class always takes an auto Keyword.

What is Constructors? Type of Constructors.

By Dinesh Thakur

A Constructor in C++ is a special member function having the same name as that of its class, which is used to initialize some valid values to an object’s data members. It is executed automatically whenever an object of a class is created. The only restriction that applies to the constructor is that it must not have a return type or void. The compiler automatically calls the constructor, and it is usually used to initialize values. The compiler distinguishes the constructor from other member functions of a class by its name, which is the same as its class. ctor is an abbreviation of constructors in C++. [Read more…] about What is Constructors? Type of Constructors.

Explain about Access Modifiers or Visibility Controls

By Dinesh Thakur

Access Modifiers :- These are also Called as Access Visibility Controls  means they defined where a method and Data Member of class will be used either inside a  class ,outside a class ,in inherited class or in main Method They Tells us the Scope of Methods where they would be used Various types of Access Modifiers  are as follows:-

 1) Public Access: – Public Access modifiers Specifies that data Members and Member Functions those are declared as public will be visible in entire class in which they are defined.  Public Modifier is used when we wants to use the method any where either in the class or from outside the class. The Variables or methods those are declared as public are accessed in any where , Means in any Class which is outside from our main program or in the inherited class or  in the class that is outside from our own class where the method or variables are declared.

 

2) Protected Access:- The Methods those are declared as Protected Access modifiers are Accessible to Only in the Sub Classes but not in the Main Program , This is the Most important Access Modifiers which is used for Making a Data or Member Function as he may only be Accessible to a Class which derives it but it doesn’t allows a user to Access the data which is declared as outside from Program Means Methods those are Declared as Protected are Never be Accessible to Another Class The Protected will be Accessible to Only Sub Class and but not in your Main Program.

 

3) Private Access:- The Methods or variables those are declared as private Access modifiers are not would be not Accessed outside from the class or in inherited Class or the Subclass will not be able to use the Methods those are declared as Private they are Visible only in same class where they are declared. By default all the Data Members and Member Functions is Private, if we never specifies any Access Modifier in front of the Member and Data Members Functions.

Data Abstraction in C++

By Dinesh Thakur

Object Oriented Programming has a special feature called data abstraction. Data abstraction allows ignoring the details of how a data type is represented. While defining a class, both member data and member functions are described. However while using an object (that is an instance of a class) the built in data types and the members in the class are ignored. This is known as data abstraction. This can be seen from the above example.

Example

An example to show how public member data and public member functions are accessed outside the class.

 

#include <iostream.h>

#include<conio.h>

class base

{

       public: int sl,s2;

       public:

       void inp_val()

        {

               cout<<“input the values of sl and s2 “;

               cin>>sl>>s2;

        }

      void disp()

       {

              cout<<sl <<” “<<s2;

              cout<<“\n” ;

       }

};

void main()

{

       clrscr();

       base o1;

       o1.inp_val();

       o1.disp();

}

The first occurrence of the keyword public (or private) before a member data or a member function makes the compiler assume the types of accesses of all the members following this to be of the first occurrence type. However, if the types of accesses of member data and member functions are different, then the access type should be explicitly mentioned in each case.

Example

An example to show that private member data cannot be accessed outside the class if the member functions which involve this data are also private.

 

#include <iostream.h>

#include<conio.h>

class base

{

          private: int sl,s2;

          void inp_val()

          {

                    cout<<“input the values of s1 and s2 “;

                    cin>>sl>>s2;

           }

 

          void disp()

          {

                  cout<<sl <<” “<<s2;

                  cout<<“\n” ;

          }

};

void main()

{

        clrscr();

        base o1;

        o1.inp_val();

        o1.disp();

}

On the execution of the above program, following error messages will appear.

error messages

Error oop22.cpp 26 : ‘base::inp-val()’ is not accessible

error oop22.cpp 27: ‘base::disp() ‘ is not accessible

Example

 

Modified version of Example in which member data are private but the member function which invoke the private data members are public. This will not cause any error as in example

#include <iostream.h>

#include<conio.h>

class base

{

          private: int sl,s2;

          public: void inp_val()

          {

                     cout<<“input the values of sl and s2 “;

                     cin>>sl>>s2;

           }

          void disp()

          {

                   cout<<sl <<” “<<s2<<“\n “;

          }

};

          void main()

          {

                 clrscr();

                 base o1;

                 o1.inp_val();

                 o1.disp();

          }

What is Encapsulation in C++

By Dinesh Thakur

Encapsulation is a mechanism that binds together data and functions that manipulate a single logical unit. It keeps the data safe from any external interference and misuse. In other words, Encapsulation is the wrapping up of data and related functions in a single unit called class. The only way to access the data is through the function, which is wrapped in the class. These functions prepare the class’s interfaces through which the outside world (other classes) can access them. [Read more…] about What is Encapsulation in C++

What is C++

By Dinesh Thakur

A Program is a set of Instructions and a Programmer is that who develops the program .For creating a program a programmer must use a language called as the programming language So generally a programming language is used for creating the software’s.

 C++ is an Enhanced version of c Language which is developed by Bjarne Stroustrup in 1980 in AT & T’s Bell Lab. C++ Inherits many features from C Language and it also Some More Features and This Makes C++ an OOP Language. C++ is used for Making Some Code which May used by another Peoples for Making their Applications .C++ Provides Reusability of Code for Another user. C++ is also Known as Object Oriented Language or Simply OOP Language because it Provides Capability to either Make Stand alone Program or either Make a Reusable Code for Another Users.  

Difference Between Procedure Oriented Language and Object Oriented Language

By Dinesh Thakur


 

Procedure oriented programming

 

Object Oriented programming

 

 

Emphasis is on doing things (algorithms)

Large Programs are divided into smaller programs known as functions.

 

Most of the functions share global data.

 

Data move openly around the system from function to function.

 

Functions transform data from one form to another.

 

Employs top down approach in program design.

 

 

 

Emphasis is on data rather than on procedure

Programs are divided into objects

Data structures are designed in such a way that it characterizes the object

 

Functions that operate on data are ties together in a data structure called class

 

Data is hidden and cannot be accesses by external functions.

 

Objects may communicate to each other with the help of functions.

 

New data and functions can be easily added whenever necessary

 

Follows bottom-up approach.

 

 

What is Pseudo Codes

By Dinesh Thakur

As we know that the Language which is used by a Algorithms is not Specific , means there is no Language which is used by a Algorithm .

In the Algorithms we just write the Instructions means we just writes the Code of a Program, and in the Programming Language , we uses the instructions those are understands by the Language. But Mostly Algorithms Contains instructions having, user instructions and also Some Programming Code. So that a Pseudo Code is that which contains the Words those is used in Algorithms. Pseudo Codes are nothing m they are the Simple Words which Contains the Instructions, having Some Special Words. A user can use those Words for Writing a Algorithm.

What do you means by C++ Tokens? Explain Variable,Data Type,Constants, Identifiers and Keyword

By Dinesh Thakur

As we know that Software is a Program. And a Program is that which Contains set of instructions, and an Instruction contains Some Tokens. So Tokens are used for Writing the Programs the various Types of Tokens those are contained by the Programs. [Read more…] about What do you means by C++ Tokens? Explain Variable,Data Type,Constants, Identifiers and Keyword

What is Instructions in C++

By Dinesh Thakur

There are many different types of Constants variables and Keywords in c++ Language. There are basically four types of Instructions in C++.

 

Ø    Type Declaration Instructions

Ø    Input/output Instructions

Ø    Arithmetic Instructions

Ø    Control Instruction

 

1.    In the Type Declaration generally variables are declared

2.    In Input and output instructions we performs input data to the program and also obtaining the output Results from it.

3.    Arithmetic instructions are used for performing the Arithmetic operations like addition, subtraction and Multiplication etc.

4.    Control Instructions are used for controlling the execution of the Program or these are used for changing the Sequence of the Program.

 

A Simple Program That is Written in C++ Language

By Dinesh Thakur

[Read more…] about A Simple Program That is Written in C++ Language

What is a Compiler

By Dinesh Thakur

We know that a computer can understand only Machine Language But the Program Written by a user is in the Form of English Language So it very necessary to convert the human Language into the Machine Language. So for this purpose we use Compiler which Converts human code into the machine code.

Compiler complies the whole program and it also display all the errors those are occurred in the Program But the disadvantage of Compiler is that it converts whole Program a time It doesn’t provide the facility to check a statement at a time.

When a computer programmer writes a program, the programming language use has some resemblance to human speech–even we nonprogrammers can read some of the words, like if and then and do, and others, like printf or struct, almost look like real words. But a computer can’t understand anything about a program written in a programming language, not even the plus signs. In order to run that program, the programmer has to first convert it into computer-ese, known as machine code, using a special program called a compiler. Usually, the compiler produces an intermediate form of the program which is then converted to the final, working form by a “linker,” another special program.

 

As a verb, to compile a program is to convert it into machine code using a compiler. Contrast compiler with interpreter.

 

What is a Interpreter

By Dinesh Thakur

We know that a Compiler compiles the whole program at a time means that it converts the whole program in single time and then gives us the errors But a interpreter is that which converts the human language into the machine language line by line and displays the errors after checking a single line.

What Do you Mean By Preprocessors

By Dinesh Thakur

Preprocessors are the Source of our Program those are Executed whey they are passing to Compiler and Preprocessors are always executed at the Time of Compilations. Preprocessors provide many features called Preprocessor directives and Each Preprocessor is declared with # symbol and This Directive must be written before a main program.

Programs starts with code line #include <stdio.h>. Here, the directive #include is a preprocessor command. All preprocessor commands start with the sign#. The command given above directs the compiler to include the header file <stdio.h> (standard input/output header file) from C Standard Library in this program. Standard input is used to denote input from keyboard and standard output is used to indicate display on the monitor connected to the computer. A program that requires standard input and standard output must include the header file <stdio.h> as it has been done in Program. The header file <stdio. h> also contains many other functions that help in formatting the input/output of a program as well.

More than one header file from C Standard Library may be included in a program if these are required for the manipulation of data of the program. For example, if you want to carry out evaluation of mathematical functions such as square-root, sine or cosine of angles, log(x), or evaluation of exponential functions, you must include the header file <math.h>.This header file has the procedural software for evaluations of many such functions. For including more than one header files in a program, these should be written in successive lines. Only one header file is written in one line. The name of each header file, if it belongs to the C Standard Library, is enclosed between the angular brackets < >. The names end with extension (.h) as illustrated below.

#include <stdio.h>

#include <math.h>

The Preprocessor’s are Used For Two Purposes either they are used as Macros and either they are used as File inclusions like Many Times we write #include this is also a Preprocessor directive which is used for Including the Contents of a File Like iostream.h and either conio.h or any other File But the Macros are Also Called as Small Programs or The Macros are Used for Giving a Short names to Long Statements and Long Keywords and a user can Specify his own names to a keyword of c language

Macros are always define by using a #define Statement and For Defining our Own Macro First we have to specify our name for a Macro then we writes the Statements those are to be Executed When a Macro is Called or When that Name is used in any where in a program For Defining a Macro We have to Write Like This

  #define Integer int

Now we can use Integer instead of int .But Always Remember we are not Replacing a Word with our Words We are just Giving a another name to a built in Keyword

In Macro we can also write any Statements and any Expression for Example

  #define Max 100

  #define or ||

  #define Sqr(x) x*x

These are the Some Examples of Writing a Macro in the First Macro we defines a Max Word Wherever we use Max then Compiler will treat as 100 and Second Macro is or name to a or Symbol means Now Whether We can use a or Symbol and Instead of Putting a Symbol we can and either write a or word. Third Macro defines a Sqr(x) Whenever we writes Sqr with a Number then this will gives us a Square of a number etc.

There are many Conditional Compilation Macros those are used for Performing operations on Macros Like Checking the Value of Macro and To Check Whether a Macro is Defined or not

The Various Condition Compilation Macros are as follows:-

1. #ifdef:- This is used for Checking either a Macro with a Specified Name is Defined or not .Or it checks Whether a Macro is defined For use.

2. #undef:- This is Used for Un-defining a Macro. Or if we want to remove the definition of a Macro from our Program .then we can use #undef.

3. #ifndef:- This Condition Compilation is used for Check either a Macro is not Defined if a Macro really not defined then this will gives us true or if a Macro is defined then this will gives us false

4. #if :- #if condition Compilation Macro if used for Checking a value of Macro that is defined in a Macro definition or it checks whether an Expression evaluates to nonzero or not and always Remember Every #if Macro is must be Closed with the help of #endif

5. #else:- Used the Condition of First #if Macro is False . This is Similar to simple If else but Difference is that if –Else operates on value of Variable but #if performs or it either checks the Value of a Macro and this will be Executed When no Match of Macro Value is to be found same as else statement

6. #else if:– This Directive is used when we wants to Check one more Condition upon the Macros When #if is False then the Compiler will check another value that is defined in #else if Directive and also Every #elseif directive must be end with #endif

What do you mean by typedef

By Dinesh Thakur

The typedef (meaning type definition) command allows the programmer to define a new name for a particular type.

In reality, this keyword to create a synonym, which is different from creating a new type. typedef is followed by the name of the existing type and then alias and a semicolon. Its general form is:

typedef unsigned short int USHORT;

Now there is also another data type which is known as USHORT and which is the Reference to the unsigned short int. Means Now we can also use USHORT instead of unsigned short int data type.

// Demonstration of the use of the keyword typedef
#include <iostream.h>
#include<conio.h>
typedef unsigned short int USHORT; // alias definition
int main ()
{
   clrscr();
   USHORT width = 5;
   USHORT length;
   length = 10;
   USHORT Area = width * length;
   cout << "Width :" << width<<endl;
   cout << "Length :"<< length <<endl;
   cout << "Area :"  <<Area << endl;
   return 0;
}
This program producesthe following result

Typedef

« Previous Page
Next Page »

Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW