DirectorySoftwareBlog Details for "Learning Computer Programming"

Learning Computer Programming

Learning Computer Programming
I post articles mostly on Learning Computer Programming in general and specifically using C plus plus. This blogsite is created as a part of my hobby of computer programming, I also share my experiences in a teaching manner.
Articles: 1, 2, 3, 4, 5, 6

Articles

Introduction to Constructor and Destructor functions of a Class
2007-06-18 07:59:00
Constructor and Destructor functions are Member Functions of a class having some special property. Constructor function gets invoked when an object of a class is constructed (declared) and destructor function gets invoked when the object is destructed (goes out of scope). Use of Constructor and Destructor function of a class Constructor function is used to initialize member variables to pre-defined values as soon as an object of a class is declared. Constructor function having parameters is used to initialize the data members to the values passed values, upon declaration. Generally, the destructor function is needed only when constructor has allocated dynamic memory. Defining Constructor and Destructor functions The example below illustrates how constructor and destructor functions are defined: class myclass { private: int number; public: myclass()//constructor { number=10; } ~myclass()//destructor { //nothing needed } }; A few poin...
More About: Classes , Introduction , Class , Const
Introduction to Function Overloading in C++
2007-06-17 07:57:00
Let us start this with a question! All of you know that we cannot have two variables of the same name, but can we have two Function s having the same name. The answer is YES, we can have two functions of the same name by a method known as function overloading and the functions having the same name are known as overloaded functions. So, what?s the use of Function Overloading Function overloading is one of the most powerful features of C++ programming language. It forms the basis of polymorphism (compile-time polymorphism). Most of the time you?ll be overloading the constructor function of a class. How function overloading is achieved One thing that might be coming to your mind is, how will the compiler know when to call which function, if there are more than one function of the same name. The answer is, you have to declare functions in such a way that they differ either in terms of the number of parameters or in terms of the type of parameters they take. What that means is, nothing sp...
More About: Introduction , Intro , Loading
Unit Conversion Program using Classes
2007-06-16 08:43:00
Welcome back guys, I hope you like my previous article, Introduction to Class es in C++, in this article we will be further continuing our discussion on classes in C++. We will not be discussing anything new on the topic though, just a simple example program to elaborate more on Classes in C++. In this article, I will show you a program based on classes, which converts units of length from one to another. The program is simple and I do not think there is any need for discussing it in detail. The example program is given below: //C++ Program #include<iostream.h> class length { private: float in_meter, in_cm, in_inch; public: void input(float len,int ch); float output(int ch); }; //---function definition starts--- void length::input(float len,int ch) { switch(ch) { case 1://for meter in_meter=len; in_cm=in_meter*100; in_inch=in_cm*2.54; break; case 2://for cm in_cm=len; in_meter=in_cm/100; ...
More About: Objects , Conversion
Introduction to Classes in C++
2007-06-15 09:00:00
Classes are the building blocks of Object Oriented Programming in C++ language. Class gives us the ability to simplify certain types of programs by creating objects that forms the basis of Object Oriented programming. Just like identifiers of built-in data types (ex. int, char etc.) are known as variables similarly identifiers (instances) of class are known as Objects. Classes in C++ have two parts, data and functions; these are known as data members and member function respectively. Member functions are usually the means of accessing and modifying data members. Anything inside the class can be either private to the class or public to the program. Classes may have both type of members (private and public) at the same time. General form of Class: class class_name { access-specifier: member… member… member… access-specifier: member… member… member… }object-list; Few points to remember: Acc...
More About: Classes , Introduction , Class , Intro
Method of Adding Individual Digits of a Number
2007-06-13 08:01:00
NOTE: The program discussed in this article has no use in practice but it would definitely help you understand a few things, so please read on? What this program does is that it takes a number as input (such as 12345) and outputs the sum of the individual digits, which in this case is 15 (1+2+3+4+5).For achieving the required result, we need to do two things, first we should store each of the individual digits in the number and second, we need to add those numbers, which will give us the required result.Let us move on to the first stage where we need to separate the digits. Here are the steps:- STEP 1: We declare a variable num as integer type and an array sep again as integer type. Suppose num=12345 STEP 2: We will be fetching digits from the right (5) to left (1). For this, we use the modulus (%) operator which divides two numbers, giving us the remainder. sep[0]=12345 % 10 Now, num=12345 sep[0]=5 (last digit has been fetched) STEP 3: Now num is divided by 10 and stored ...
More About: Individual , Gits , Dual , Method , Number
Simulating the throw of dice in C++
2007-06-11 07:51:00
What makes the throw of dice special is the fact that you can?t always guess which number will come up. This property is known as randomness, which is needed in certain types of programs (ex. Games).In this article, we are going to discuss how random numbers are generated and used in C++ with the help of a simple example.The program illustrates generation and use of random numbers in C++: //C++ Program to generate RANDOM NUMBERS //function is isolated so it may be easily //incorporated in other programs //NOTE:stdlib.h header file is required //for using this funtion elsewhere #include<stdio.h> #include<stdlib.h> #include<conio.h> //FUNCTION PROTOTYPE int random(int HighestNumber,int LowestNumber); void main(void) { char ch; puts("Random Number Generator: "); while(ch!='q' && ch!='Q') //loop until q or Q is pressed { puts("Press any key to throw dice..."); ch=getch(); //%d tells the compiler that the value after ...
More About: Throw , Dice
String Searching Function in C++
2007-06-10 06:42:00
Nothing much to say, here I present you with a searching function. It takes two-character string (array) as the argument and finds the position of the second string in the first. For example, if the two arrays passed to the function have the following values:String 1: ?I like C++? String 2: ?C++?then the function will return 7, because as an array, string 1 has the word C++ starting from the index number 7.If the function cannot find the second string inside first then it will return the value -1, indicating that the search was unsuccessful.The program below is easy to understand; therefore, I have left it up to you to understand how it is working. //C++ program which searches for a substring //in the main string #include<stdio.h> int search(char *string, char *substring); void main(void) { char s1[50], s2[30]; int n; puts("Enter main string: "); gets(s1); puts("Enter substring: "); gets(s2); n=search(s1,s2); if(n!=-1) { ...
More About: Search , Searching , Function , Tring
Sorting an array using Selection Sort
2007-06-09 09:37:00
Sorting is the process by which the elements of a group (ex. Array s) are arranged in a specific order (ascending or descending).The process of sorting is very important, since it is needed in many types of programs. Ex. If you need to arrange the heights of various students in a class, you need to sort their heights.While there are quite a few methods employed for sorting, we will be discussing about Sele ct ion Sort method in this article.In selection sort, all the elements of an array are compared with the other elements following it and interchanged so that smaller elements come at the top.So, what that means is that the first element is compared with the second, third ? elements then the next is selected (second element of the array) and it is compared with the third, fourth ? elements and in this way different elements are selected serially and compared with elements following it. Interchange is done so that smaller elements come high up in the array. (for ascending order)To mak...
More About: Sorting , Sort
Simple Problems in C++ Part III
2007-06-07 08:58:00
Yesterday I was having a look at my traffic statistics when I came to know about one thing. I saw that most of the visitors to this blog paying attention to the two of my recent articles Simple Problem s in C++ Part I and Simple Problems in C++ part II. Seeing that, I made up my mind to write the third part of the Problems in C++ series with some more interesting problems. Now, without wasting your time anymore, I present you with some more Simple problems in C++. Here they are :- Problem No. 1: #include<iostream.h> void main(void) { int i=1; for(;;) { cout<<i++; if (i>10) break; } } QUESTION: Is there any error in the program? Problem No. 2: #include<iostream.h> void main(void) { int i=1; while() { cout<<i++; if (i>10) break; } } QUESTION: Is there any error in the program? Problem No. 3: #include<iostream.h> void main(void) { int a=10,b=20; if(!(!a) && a) ...
More About: Question
Name Abbreviation Program in C++
2007-06-07 08:53:00
In this article, we are going to design a program that abbreviates the first and middle name leaving the last name as it is. Suppose if the user inputs the following name: William Henry GatesThen the output of the program would be: W. H. GatesWithout further a due I present you with the Steps:-We have declared two character arrays (strings), name[4] and abname[30], which stores the name input by the user and abbreviated name respectively. Suppose if the user inputs the following name: William Henry Gates Then the two arrays would have the following values: Name[40]="William Henry Gates" (unchanged) Abname[30]=EMPTY The next step is to store the first character of the array name[40] to the abname[30] array and put a dot (.) and a space( ) after it. Next, we set-up a loop which finds space in the array name[40] (space separates first, middle and last names). As soon as a space is detected the character immediately after the space is stored to the abname[30] array, followed by...
More About: Program , Gram
Learn to Define and Use Functions in C++
2007-06-06 08:28:00
Functions can be thought of as separate programs that are designed to perform specific tasks. For example, in a program, separate functions can be so designed to perform specific tasks such as taking input, doing calculation, giving output etc.In this article we will be discussing about what functions are and how they are designed in C++.Generally, programs have many logically different parts (input, output etc.) which can be coded separately as function. This makes programs easily manageable and understandable.Suppose we have to design an invoice program that takes input, calculates and at last displays the output. We have two options to do so, we can either program the whole thing linearly or we can make separate functions for each of the tasks (input, output etc.), this would make the program far more manageable and upgradeable. (In the future, if you need to change certain things, you only need to alter the functions)In this way if you divide various activities of the program in...
More About: Functions , Learn , Fine , Function , Define
Learn to use Loops in C++
2007-06-05 08:47:00
While every programming language has its own version of loops (iteration statements), the loop statements of C++ are way much more powerful than those of other languages. It is because of the variations that loops can have in C++. This article would give just an introduction to the loop structures in C++, leaving complex variation of loops for the coming articles. A Loop (iteration) statement repeatedly executes a set of statements until a particular condition is reached. Have a look at this program: //C++ program #include<iostream.h> void main(void) { int arr[3]; cout<<"enter three numbers:"<<endl; cin>>arr[0]>>arr[1]>>ar r[2]; cout<<"number 1: "<<arr[0]<<endl; cout<<"number 2: "<<arr[1]<<endl; cout<<"number 3: "<<arr[2]<<endl; } Now look at this program: //C++ program #include<iostream.h> void main(void) { int arr[3]; cout<<"enter ...
More About: Learn , Loops
Learn to use Pointers in C++
2007-06-04 13:50:00
For those of you who do not know pointers are, I give you a short definition. Pointer is a type of variable that stores memory addresses rather than simple data. Pointers also have Data Types associated with it, Ex. int pointers can only store memory address of int variables and similarly float pointers can only store memory address of float variables. Pointers are one of the most important distinct features of C++. It gives the programmers power and control over several things. Pointers have many uses; sometimes it is used as the faster means of accessing arrays while sometimes it is implemented so that the function in C++ can modify the parameters passed to it. Pointers are extensively used in C++, this might not be obvious to you right now but as you start programming some complex program, you will notice how important pointers are. This article would serve as a little introduction to pointers in C++, it would help you understand what pointers are and how it is used in C++. Two ...
More About: Learn
Operators in C++ Part II
2007-06-02 06:56:00
In the previous article, we had been discussing about what operators are and the types of operators in C++. In that article, we discussed till arithmetic and relational operators, in this article we will continue our discussion with operators in C++ by moving on to the other types of operators. Logical Operator s In the previous article, we discussed about relational operators that eastablish relationships among the operands, logical operators refers to the ways by which these relationships can be connected. (Logical OR), && (Logical AND), ! (Logical NOT) are the three types of logical operators. These are used to combine existing expressions. The examples below will illustrate the working of each of these operators. (4<5)(5<4) this expression results in 1 (true), since one of the expression (first one) is true. (6<8)&&(4==4) results in 1 (true). (6<8)&&(4<3) results in 0 (false), because one of the relation is false. !(4<8) results in 0 (false). !(4&g...
More About: Part
Operators in C++ Part I
2007-06-02 06:36:00
C++ has a vast range of operators, in fact C++ has greater number of operators than most other programming languages. Operator s in C++ are divided into categories namely arithmetic, relational, logical, conditional, bitwise etc. according to the type of operation they perform. Arithmetic Operators in C++ +, - ,*, /, % are all arithmetic operators. You should not be having any problems with the first four operators (they have their normal meaning), regarding the last one, it is known as the Modulus operator. It divides two operands (data, variable) and gives the remainder of the two upon division. Increment (++) & Decrement (--) Operators The increment operator acts on one operator to increase its value by one while the decrement operator does the opposite. Prefix (++var) and postfix (var++) are the two types of increment/decrement operators. Both does the same arithmetic operation to the operand but the value of the variable in the expression is evaluated differently in different c...
More About: Part
Simple Problems in C++
2007-06-01 05:39:00
Better programmer is he who has better programming skills, for programming skills one needs to PROGRAM because only reading won?t get you anywhere. From regular programming comes good programming skill. Solving problems basically means to write a program which performs the actions as per the question. But the problems listed here are different; these problems will check your understanding of the C++ programming language. You don?t need to write any program. In this article I have listed some of the good problems that I have come across, which are related to what we discussed so far (variables, data types etc.). This article is basically for beginners but even if you are an experience C++ programmer, you might want to look at them. Problem No. 1 //C++ Program #include<iostream.h> void main(void) { int arr[5]={2,3}; cout<<arr[2]<<arr[3]<<a rr[4]; } QUESTION: What will be the output of this program? Problem No.2 //C++ Program #include&...
More About: Questions , Problems , Simple
5 Common C++ Programming Mistakes
2007-05-31 06:16:00
To err is human It is normal for programmers to make mistakes while learning c++ programming. Mistakes (or errors) are often caused by misconception in the programmers mind or by forgetting certain things. While syntax errors are easier to spot since the c++ compiler shos the related information but logical errors often remains unnoyiced, and many a times the program works just as fine. It is not possible to compleletly eliminate errors by yourself but it is definitely possible to minimize them as a c++ programmer. Here I am listing 5 of the common mistakes (errors) that beginners commit (not sorted in any order). Mistake No. 1 Have a look at this C++ Program #include<iostream.h> void main(void) { int base,height,area; cout<<"Enter the base and height of the triangle:"; cin>>base>>height; area=0.5*base*height; cout<<endl<<"AREA:"<<ar ea; } Here the variable area is such that sometimes its value would be whole n...
More About: Programming , Common , Comm , Ming , Gram
Simple Problem in C++
2007-05-30 05:55:00
Interchanging the values of two variables? What?s the big deal. You take one extra variable; put the first variable?s value in it, copy the value of the second variable to the first one and then copy the value of the extra variable to the first one and there you are! But what, if I say that you don?t have to use the third variable, let?s say because the memory is full and you are left with memory only sufficient for storing two variables. Doesn?t make sense. Huh! It makes sense if you want to gain programming skills because solving problems is the only way to do it. In this article we start with a simple one. So, how do we do it? It is not very big deal to interchange the values of two variables without using the third variable yet I have come across peoples who don?t think it can be done. To solve this problem we need to do two things (i)First we need to write down the method (algorithm) of doing it (ii0 Secondly, we need to write the c++ code from the algorithm. The Method The...
More About: Problem , Simple
Half an Hour to Variables
2007-05-29 07:55:00
I wrote this article yesterday when I was getting bored and was feeling too lazy, all thanks to the hot weather here. I planned of meeting my friends but at last I insisted on staying at home when I wrote this. Regarding the title, I thought that if it took me an hour to write it then you would have to hardly give Half an Hour to Varia bles . Now coming to the serious stuff? What is Variable During program run we need to manipulate data and the data to be manipulated are stored in memory locations known as variable. In the system memory, data are stored in memory locations such as 0x00243 which are not easy to remember if we have quite a lot of them. Variables are memory locations having a name so that different memory locations can be distinguished easily. Declaration and Initialization of Variables The declaration of variable generally takes the following form: type varname; Here type is any valid data type and varname is the variable name. Ex.int age; float temp; Static initi...
C++ Data Types in Detail
2007-05-28 05:21:00
Data types are means to identify the type of data and associated operations of handling it. C++ provides a predefined set of data types for handling the data it uses. When variables are declared of a particular data type then the variable becomes the place where the data is stored and data types is the type of value(data) stored by that variable. Data can be of may types such as character, integer, real etc. since the data to be dealt with are of may types, a programming language must provide different data types. In C++ data types are of two types:- Fundamental Data Types : As the name suggests these are the atomic or the fundamental data types in C++. Earlier there was five of these (int, char, float, double and void) but later two new data types namely bool and wchar_t have been added. Int stores integer data or whole numbers such as 10, -340 etc., char stores any of the ASCII characters, float is used to store numbers having fractional part such as 10.097, double stores the s...
More About: Data , Tail
A Tour of Computer Programming
2007-05-23 07:43:00
You might have read many so-called “the art of computer programming an” and many “how to …” on computer programming, but this one is different. It doesn't’t deals with the technical details of programming rather it deals with what is more important to beginners. This article would help beginners who are having a tough time with programming to get to the right track. It is also for newcomers who want to start programming and want to choose the right programming language. It is not just another guide to learn computer programming; it helps you to minimize your confusions while learning computer programming. It is not an article which talks about the history or something like that, it is a practical guide. This article is written keeping in mind what the beginner’s confusions are and how to minimize them. Careers in Computer Programming As a professional having a degree from any reputed institution you have a good chance of maki...
More About: Tour , Ming , Gram
Introduction to C++ Programming
2007-05-22 05:55:00
This article gives you an introduction to C++ Programming from ground level. This article won't teach you all the fundas of C++ programming rather it gives you the base to learn C++ programming, remember that for further learning, the base should be strong and this is what this article tries to do. It would let you know many fundas which will help you in further learning of the the language. C++ was developed in 1980s in the Bell Laboratories by Bjarne Stroustrup as an object oriented programming language. This language is considered by many as an extension of the programming language C. The extension of programming language C to create C++ is obtained by adding classes to C. This is why C++ was initially called ?C with Classes?. The C++ programming language derives its name from the increment operator used in C, which increments the value of a variable. The symbolic name of C++ rightly indicates that this language is enhanced version of C. Features of C++ Programming Language:- ...
More About: Introduction , Intro , Ming , Gram
More articles from this author:
1, 2, 3, 4, 5, 6
46837 blogs in the directory.
Statistics resets every week.


Contact | About
© Blog Toplist 2008 - Supported by Web Catalog - SEO by FeWorks
eXTReMe Tracker