# Learning C++: How to Program Conceptually

This article is intended for readers who have some knowledge with C language or even C++. We will focus on some examples in order to understand programming better and also utilize the concept of object-oriented programming.

C++ is an object-oriented programming language developed by Bjarne Stroustrup. C++ is a superset of C. In simpler terms, C++ is an ‘extended version’ of C language. C does not support *class *or *templates*. Because of the introduction of ‘class’ in C++, it is also known as ‘C with classes’. The etymology of C++ (pronounced as C plus plus) is based on the increment operator (++) which increases the value of a variable by 1. Since C++ is a successor of C language with different new features and functionalities, it’s logical to call it C++.

Enough with the introduction; let’s begin programming. First, we will write a program which converts a decimal number into its fractional form. We will call it DecimalToFraction.

Examples:

*User enters 2.5 as the decimal number*

*The fractional form for this number is 5/2*

*User enters 34.46 as the decimal number*

*The fractional form for this number is 1723/50*

This program is meant to develop our programming skills. It involves the use of a class, and the mathematical concept of HCF (Highest Common Factor). For any given two or more integers, HCF is the largest positive number which divides all of the numbers without leaving any remainder.

## DecimalToFraction.cpp

#include <iostream> using namespace std; class FractionIt { public: int numerator, denominator; double number; FractionIt(double n) { number=n; } void setNumAndDen() { double divisor=1,multi; do { divisor=divisor*10; multi=number*divisor; } while (!isDivisible(multi)); numerator=multi; denominator=divisor; } bool isDivisible(double,double val=1); //declaration of method Divisible void toFraction() { int MaximumDivisor=denominator; int num=numerator,den=denominator; for(int i=2;i<=MaximumDivisor;i++) { if(isDivisible(numerator,i) && isDivisible(denominator,i)) { num=numerator/i; den=denominator/i; } } cout<<"The fractional form is "<<num<<"/"<<den; } }; bool FractionIt::isDivisible(double num,double div) //definition of method Divisible { int absvalue; double extvalue; absvalue=num/div; extvalue=num/div; if(absvalue==extvalue) return true; else return false; } int main() { double inputNumber; cout<<"Enter the number: "; cin>>inputNumber; FractionIt oNumber(inputNumber); oNumber.setNumAndDen(); oNumber.toFraction(); }

## Basics

Now let’s discuss the parts of the program one by one.

Class FractionIt

This class is the heart of our program. A class in programming refers to a collection of functions (called methods) and variables which can be assigned to an entity (called object). Call them member methods and member variables. We assumed the readers to have brief knowledge of C/C++ programming, but let us warm up.

## A little warm-up

Say, there is a pizza cafe with 10 customers. Everyone has the menu list at their tables. The menu says “Pizzas” and a list of pizzas available is provided. Some select the first item, some the last while some select multiple items. Now, the menu is a class, and the items are the member methods. The customers are objects who called for different items (methods). The behavior of the menu never changes, it remains the same. But, the customers can use it to get different results, although all of them have come to eat pizza.

Let’s convert the above example into code:

## Pizzas.cpp

#include <iostream> using namespace std; class Pizzas { public: int cheesePizza, thinCrustPizza,specialPizza, coupon; Pizzas() { cheesePizza=1; thinCrustPizza=2; specialPizza=3; } int getCheesePizza() { return cheesePizza; } int getThinCrustPizza() { return thinCrustPizza; } int getSpecialPizza() { return specialPizza; } void setCoupon(int codeNumber) { coupon=codeNumber; } }; int main() { Pizzas customer1; cout<<"Customer 1: "<<endl; cout<<customer1.getCheesePizza()<<endl; Pizzas customer2; cout<<"Customer 2:"; cout<<endl<<customer2.getSpecialPizza()<<endl; Pizzas customer3; cout<<"Customer 3:"; cout<<endl<<customer3.getThinCrustPizza(); cout<<endl<<customer3.getCheesePizza(); customer3.setCoupon(247); }

Three objects of Pizza class have been declared. Each object calls a different method. Thus, each object performs some different activity from the same common class. Since our customer3 ordered for 2 pizzas, he got a coupon with a code in it. Then, he gave the code to the cashier who will check for any special prizes the café has to offer. This was done by the setCoupon () method. Also, note that the methods are called by adding a dot operator and the method name at the end of the object.

Let’s get back to our FractionIt class. It has three member variables: numerator (int), denominator (int) and number (double). The variable ‘number’ stores the number entered by the user. The class has one parameterized constructor which assigns the input number to the member variable ‘number’.

As I mentioned earlier, the heart of our program is this class. Well, this heart has 3 chambers:

- setNumAndDen (void)

- isDivisible (bool)

- toFraction (void)

## isDivisible(bool) method

Let’s start with the isDivisible method. This method checks whether any given two numbers can be divided by a specific number or not. By ‘divided’, you have to understand it the number divides any other number without leaving any remainder. For example, consider two numbers 12 and 28. Other than 1, there are only two numbers in this entire universe that can divide 12 and 28 without leaving any remainder. They are 2 and 4. I know you started thinking of other numbers that may divide 12 and 28, but trust me on this one and let’s continue further. So, if you passed 12 and 28 as the two arguments to *isDivisible*, it would return *true*. We will soon come to know how *isDivisble* is connected to both *setNumAndDen* and *toFraction* methods.

## setNumAndDen() method

You will need to pay a little more attention, now that the hero chamber of our class, *setNumAndDen* will be introduced. The sole purpose of *setNumAndDen* is to convert the given number into fractional form where the denominator is always a multiple of 10. For example, 4.32 can be written as 432/100 and 3.1 can be written has 31/10. What our little hero does is check if the number is finally divisible or not. If it is not, then it multiplies the number by 10 and reiterates the whole process. Let’s show this with an algorithm.

Step 1: Assign 1 to a variable.

Step 2: Multiply it by 10 and then multiply the given number with the variable.

Step 3: Is the given number now a non-decimal number? If yes, then go to step 4, else go to Step 2.

Step 4: That’s it. No more steps.

As you noticed, *setNumAndDen* method uses *isDivisible* too. However, it uses it in a different way. Notice that *isDivisible*’s second parameter has a default value i.e. 1 for the denominator. Even if only one argument is passed to this method, the compiler will not show an error because this is completely valid in C++ (Note: this is not valid in Java). So, how does *isDivisible* check whether the number is non-decimal or not when the second parameter is 1? The variables ‘*absvalue’* and ‘*extvalue’* in indivisible are of *int* and *double* types respectively. One limitation of *int* type is that variables of this type cannot store decimal numbers whereas a *double* type allows this. Also, C++ allows comparison of these types. Say, if the number passed to *isDivisible* as the first parameter is 4.2. An *int* variable would store 4 if 4.2 was assigned to it because it doesn’t store decimal numbers. But, if 4.2 was assigned to a *double* variable, then it would store 4.2 without altering the value. Now let’s pass 42 as the first parameter. Both the variables are going to store 42 without any alterations. This means, the number is indeed divisible by some integer other than 1.

When a number is found which is divisible, the loop breaks. The loop gives us two new numbers: The denominator and the numerator which when evaluated will give us the decimal number that was entered by the user. These are divisor and multi variables, respectively. We then assign these two variables to the member variables (denominator and numerator) of the object *oNumber* belonging to *FractionIt* class.

## toFraction() method

We now have a fraction representation of the given number, but still, it could be simplified. 25/10 can be simplified to 5/2. This is what our final method, *toFraction*, will be doing.

The algorithm of *toFraction* is quite simple. A loop checks whether the denominator and numerator can be divided by some other number. It keeps checking for the number which may range from 2 to the value of denominator. For example, if the numerator and denominator are 10 and 4 respectively, then the maximum value of that particular number is 4 and the minimum is 2.

First, the numbers are divided by 2 and the result is a success; both the numbers are divisible. What if there are other larger numbers that can divide these two numbers? The loop continues and divides the numbers by 3. Oops! It continues to 4. The denominator is divisible by itself, but 10 isn’t. So, our magic number is 2. Finally, the result is 5/2.This way, our program is completed.

The program is simple enough, but usually, simple is hard to obtain. This program could be written without the use of classes, but to clarify the concept of OOP (Object Oriented Programming), I made use of them.

The technique isn’t about learning some language from top to bottom; it’s about understanding the application of the language. We used common features of C++ to write the above program. So, even if you are just beginning with C++ programming or any kind of programming, always know that simple concepts can be formulated from simple knowledge. Have fun programming!

## Popular

## Comments

No comments yet.