C++ Programming Blog

Introduction to C++ Programming

As the name suggest, C++ is a programming language based on C with some added features (represented by ++) which makes possible the creation of comparatively efficient programs. C++ programming inherits all the good features of C by filtering out some of the limitations of C. In addition, C++ programming also possesses some extremely new unique concepts which were not at all present in C. However, since C++ is based on C, it is extremely important to have a thorough knowledge of C. However, by chance you are not thorough with C Programming then you are strictly recommended to have a look at Learn C Online tutorial – www.LearnCOnline.com. Remember, it is extremely important to have a strong foundation in order to build a strong structure. Now, let us turn our attention to the study of C++. So, let’s start our journey towards the study of C++…

Introduction to Inheritance in C++

Let me introduce you to the concept of Inheritance in C++. Reusability is yet another important feature of C++. It is always nice if we could reuse something that already exists rather than trying to create the same all over again. It would not only save the time and money but also reduce frustration and increase readability. For instance, the reuse of a class that has already been tested, debugged and used many time can save us the effort of developing and testing the same again. Fortunately, C++ strongly supports the concept of reusability.The C++ classes can be reused in several ways. Once a class has been written and tested, it can be adapted by other programmers to suit their requirements. This is basically done by creating new classes which reuse the properties of the existing ones. The mechanism of deriving a new class from an old one is called...

sizeof operator in C++

sizeof operator in C++ is an unary operator which can be used to find the size of the data-type or expression or a variable. It returns the size of the memory allocated (in Bytes) by the compiler. Lets say, we want to find the size of the memory allocated to the data-type int. We can find it by using sizeof operator as shown in the below code: #include<iostream.h> #include<conio.h> void main() { clrscr(); int sizeOfInt; sizeOfInt = sizeof(int); cout << sizeOfInt << ” Bytes”; getch(); } Output: 2 Bytes As you can see, the above code calculated the size of int data type and displayed the result in C++.

C++ program using two constructor to calculate minutes and seconds with hour as input from user

Write a class with name Time. Make a constructor which calculates the given time in minutes {if input time is 1hr, then the constructor should return 60minutes. }. Make another constructor which calculates the time in seconds. { if the input time is 1hr, then the constructor should return 60*60=3600 seconds}. Display the contents of both the constructors #include<iostream.h> #include<conio.h> class Time { public: Time(int); Time(float); }; Time::Time(int b) { cout << “Time in seconds: ” << b*60*60 << “n”; } Time::Time(float b) { cout << “Time in Minutes: ” << int(b*60) << “n”; } void main() { clrscr(); int hr; cout << “Enter the time in Hours: “; cin >> hr; Time t1(hr); Time t2(float(hr)); getch(); } Output Enter the time in Hours: 1 Time in seconds: 3600 Time in Minutes: 60

Program in C++ to find weekday based on weekday number

The below program in C++ will find out weekday based on the weekday number input. We will be following the below logic. 1 -> Sunday 2 -> Monday 3 -> Tuesday 4 -> Wednesday 5 -> Thursday 6 -> Friday 7 -> Saturday Here we are accepting the week day number as input from the user and use switch statement to display the weekday. Below is the program in C++. #include<iostream.h> #include #include void main(){ int day; clrscr(); cout << “Enter the day number: “; cin >> day; switch(day){ case 1: cout << “nIts Sunday”; break; case 2: cout << “nIts Monday”; break; case 3: cout << “nIts Tuesday”; break; case 4: cout << “nIts Wednesday”; break; case 5: cout << “nIts Thursday”; break; case 6: cout << “nIts Friday”; break; case 7: cout << “nIts Saturday”; break; default: cout << “nPlease enter number between 1 to 7”; break; }...

this pointer in C++

C++ uses a unique keyword called this to represent an object that invokes a member function. this pointer in C++ is a pointer that points to the object for which this function was called. For example, the function call: A.max() will set the pointer this to the address of the object A. The starting address is the same as the address of the first variable in the class structure. This unique pointer is automatically passed to a member function when it is called. The pointer this acts as an implicit argument to all the member functions. Consider the following simple example: class ABC { int a; … … }; The private variable a can be used directly inside a member function, like: a = 123; We can also use the following statement to do the same job: this -> a = 123; Since C++ permits the use of shorthand form...