Learning Computer ProgrammingLearning Computer ProgrammingI 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
Properties of Pure Virtual Functions
2007-08-11 15:51:00 In the article Properties of Virtual Functions , we discussed about two properties of virtual functions in detail. In this article we’ll be discussing about some properties of Pure Virtual Functions. Property #1: We know that a base class can’t define a pure virtual function and at the same time its derived class must define it. But what if the derived class is used as base for deriving yet another class; is this possible? Yes, it is, as is obvious from the following program: #include <iostream.h> // base class class base { public: // pure virtual function // declaration virtual void func() = 0; }; // derived class class derived1 : public base { public: // must define void func() { cout<<"Derived1's func() "; } }; // derived from class derived1 class derived2 : public derived1 { public: void func() { cout<<"Derived2's func() "; ... More About: Class , Inheritance , Polymorphism
Practical Example of Using Virtual Functions
2007-08-10 15:35:00 From the past few articles we have been discussing about virtual functions but we are yet to observe any of its practical use, this article would do that! In this article we are going to show you a very simple program that illustrates the practical use of virtual functions. Please read the code carefully! // Practical example of // when virtual functions are // used #include <iostream.h> class area { protected: int mag; double a; public: area(int x){mag=x;} double get_area(){return a;} // pure virtual function virtual void compute()=0; // it is made pure as // it couldn't have any meaningful // definition since area can only // be defined w.r.t something specific }; class circle_area : public area { public: circle_area(int x) : area(x){} // now that we are referring // to area w.r.t a circle so // it is natural that we define // it void compute() { a=(mag*mag)*3.14; ... More About: Functions , Virtual , Class , Inheritance , Function
Pure Virtual Functions
2007-08-10 15:30:00 From the previous article Properties of Virtual Functions , we know that a virtual function may or may not be overridden in the derived lasses. It means, it is not necessary for a derived class to override a virtual function. But there are times when a base class is not able to define anything meaningful for the virtual function in that case every derived class must provide its own definition of the that function. To force this type of overriding you use the following general form to declare a virtual function: virtual ret-type func-name(arg-list)=0; This type of virtual function is known as Pure Virtual Function. There are two major differences between a virtual and a pure virtual function, these are below: There CAN’T be a definition of the pure virtual function in the base class. There MUST be a definition of the pure virtual function in the derived class. By making a virtual function ‘Pure’, it become... More About: Class , Inheritance
Properties of Virtual Functions
2007-08-08 15:31:00 From the previous two articles Introduction to Virtual Function s and Virtual Functions and Run-time Polymorphism, we have been discussing about Virtual Functions. In this article we’ll be discussing about two important properties of Virtual Functions. As properties can be better understood by examples, we’ll be using them more rather than text and definitions that could confuse you. Property #1: // Properties of virtual functions #include <iostream.h> // base class class base { public: virtual void func() { cout<<"Base's func() "; } }; // derived class class derived1:public base { public: // this is a virtual function void func() { cout<<"Derived1's func() "; } }; // derived from another // derived class class derived2:public derived1 { public: // still virtual void func() { cout<<"Derived2's func() "; ... More About: Class , Inheritance
Virtual Functions and Run-time Polymorphism
2007-08-06 15:41:00 Before beginning this I would like to tell you one thing through the following program: // Virtual Functions and // Run-time Polymorphism #include <iostream.h> // base class class base { public: int a; }; // derived class class derived:public base { public: int b; }; // main void main() { base b; derived d; // base class pointer base *bptr; // pointer pointing // to base's object bptr=&b; bptr->a=10; // pointer pointing // to derived's object bptr=&d; // still is able to access // the members of the base // class bptr->a=100; } The property above combined with virtual function can be used to achieve a very special and powerful feature, known as run-time polymorphism. We had discussed about What is Polymorphism before so we wont be discussing it here. The program below illustrates how virtual functions can be used to achieve run-time pol... More About: Time , Class , Inheritance
Introduction to Virtual Functions
2007-08-06 15:38:00 Virtual functions are special member functions of a class which may be re-defined in the derived classes. It is used to give specific meaning to the base class member function with respect to the derive class. Virtual functions can be thought of as a function name reserved in the bas class which may be re-defined in the derived classes as per the need so that every derived class has the same function that performs specific (as redefined in the derived class) action. Let’s now have a look at a simple program to show virtual functions inaction: // Virtual functions #include <iostream.h> // base class class base { public: // precede the function name // with the 'virtual' keyword // to make it a virtual function virtual void func() { cout<<"Base's func() "; } }; // derived class class derived:public base { public: // redefinition of the // function void func() { ... More About: Functions , Introduction , Virtual , Class , Inheritance
Friend Functions of Class
2007-08-05 08:43:00 First, have a look at the following code: // Using Friend Function s #include <iostream.h> class myclass { int a; public: friend int geta(myclass); void seta(int x){a=x;} }; // notice how the friend function // can access even the private members // of the class int geta(myclass ob) { return ob.a; } void main() { myclass obj; obj.seta(100); // accessed as usual cout<<geta(obj); } Did you notice the specialty? In the above example program the function geta() is just a general function (friend of class, of course) but still it is able to access the private member of the class (i.e. the variable ‘a’). The function is also called as usual since it is not a member function. This is because by declaring any non-member function as friend inside a class, gives it access to the entire Private and Protected members of that class. Many of you would be wondering what is gained by doing t... More About: Class
Static Member Functions of Class
2007-08-05 08:38:00 Much like the Static Members, there also exist static member functions. Just as static members, static functions can also be accessed independently of any specific object and thus its primary use is to pre-initialize static members before creation of any object. The following program illustrates how static member functions are declared and used: // Static Member Function s #include <iostream.h> class myclass { // declare a static int a; public: // static function static void init(int x){a=x;} int get(){return a;} }; // define a int myclass::a; void main() { // static functions may // be called independently // using the class name myclass::init(100); myclass obj; cout<<obj.get(); } In the above example the static members function (init() ) is used to initialize the static member variable ‘a’ before object creation. A few points to remember: Static member funct... More About: Class
Using Static Data Members in Classes
2007-08-04 16:13:00 Let us start with a question. Suppose we want to have some information (i.e. a variable) which should be available ‘as is’ to all the objects of a particular class (ex. the number of objects of that class available at a time). Then what would you do? You can’t make it to be a regular member of the class because then, every object would have its own copy of that information which should have been common to all objects. One way of achieving this is to declare that variable as global and access it within the class wherever needed as a member. This is illustrated in the program below; it keeps track of the number of objects present (defined) at a particular time. #include <iostream.h> int obj_count=0; class myclass { public: myclass(){obj_count++;} ~myclass(){obj_count--;} int count(){return obj_count;} }; void main(void) { myclass o1; cout<<o1.count(); cout<<endl; myclass o2;... More About: Data , Classes , Variables , Class , Static
Using Virtual Base Classes to Avoid Ambiguity
2007-08-04 16:10:00 Let’s start this with the following example program: // this program contains errors #include<iostream.h> // base class class base { public: int a; }; // derived class (1) class derived1:public base { public: int b; }; // derived class (2) class derived2:public base { public: int c; }; // derived class (3) class derived3:public derived1, public derived2 { public: int d; }; // main void main() { derived3 ob; // this is ambiguous // since two copies of // base is present in // the class derived3 // one from derived1 & // the other from derived2 ob.a=10; } The program above contains an error (as stated) in the following line: ob.a=10; Since the base class is inherited by the two classes (derived1 and derived2), which are again inherited (both together) by another class derived3, there are two copies of the base class (and hence two copies of variable ... More About: Classes , Virtual , Class , Inheritance , Avoid
Problems Related to Class and Inheritance
2007-08-03 08:56:00 To make our ongoing discussion on Inheritance a little more interesting, I have listed some problems or questions here. They all are related to Class es in general and Inheritance in particular. Problem #1: This program contains some error(s), can you spot which line(s) contains error(s)? 1 // Problems No.1 2 // Related to Inheritance 3 #include<iostream.h> 4 5 // base class 6 class base 7 { 8 int a; 9 10 public: 11 void seta(int num){a=num;} 12 }; 13 14 // derived class 15 class derived:public base 16 { 17 int b; 18 19 public: 20 void setb(int num){b=num;} 21 22 void show() 23 { 24 cout<<a<<" "<<b; 25 } 26 }; 27 28 // main 29 void main() 30 { 31 base b; 32 derived d; 33 34 b.seta(10); 35 d.setb(100); 36 37 d.show(); 38 } Problem #2: Thi...
Parameterized Constructors of Derived Classes
2007-08-03 08:54:00 In the previous article Constructors and Destructors of Derived Class es , we’re discussing about the calling conventions of Constructors and Destructor functions of derived classes whose base class also had them. There was one thing special about those constructors; none of them were taking arguments. If only the derived’s constructor takes parameters then also its O. K. but what if both the base and derived class contains parameterized constructors (as is obvious from the code below). // this code contains ERRORS // base class class base { int a; public: base(int n) { //... } }; // derived class class derived:public base { int b; public: derived(int m) { //... } }; //main void main() { base b(10); //ok derived d(10); // ERROR!! // base's constructor // will also be called and // it needs parameters too !! } How’d you pass arguments to the ... More About: Functions , Inheritance , Teri
Constructors and Destructors of Derived Classes
2007-08-02 08:23:00 Classes can have Cons tructors and/or Destructors and Derived Classes are no different. Situation remains understandable until both the base and its derived class have Constructors and/or Destructors. Since the derived class contains more than one Constructors and/or Destructors, it becomes confusing which one will be called when. This is because when an object the inherited class is constructed both the constructors (base?s and its own) should be invoked and same applies when it gets destructed. This article will clear all this! Consider the following example program: // -- INHERITANCE -- // Constructors, Destructors // and Inheritance #include<iostream.h> // base class class base { public: base(){cout<<"Constructing Base ";} ~base(){cout<<"Destructing Base ";} }; // derived class class derived:public base { public: derived(){cout<<"Constructing Derived ";} ~derived(){cout<<"Destructing Derived ";} }; ... More About: Classes , Functions , Class
Inheriting from Multiple Base Classes
2007-08-02 08:20:00 In the past articles we saw how we can make a class inherit from another class. This way we could have a new class with features of the base class without explicitly defining them. But what if, we want to have a new class with features form more than one class. In other words, Is it possible for a derived class to inherit multiple base classes (two or more). Yes it’s possible! The general from for deriving a class from multiple classes is: class derived-class:access-specifier base1,access-specifier base2... { ... ... ... }; The following example program illustrates multiple inheritance practically: // -- INHERITANCE -- // Program to illustrate multiple // inheritance #include<iostream.h> // base class (1) class base1 { protected: int a; public: void showa(){cout<<a<<" ";} }; // base class (2) class base2 { protected: int b; public: void showb(){cout<<b<<... More About: Classes , Class , Inheritance , Base
Deriving a Class from another Derived Class
2007-08-01 15:49:00 Many of the peoples think that deriving a class from other derived classes is a confusing thing, that’s why I have written this article to let them know that deriving such classes is no different. To the compiler it doesn’t matter whether the class from which a new class is derived is itself derived or not. The example program below illustrates this: // -- INHERITANCE -- // Example program to illustrate // the derivation of a class // from another derived class #include<iostream.h> // base class class one { int a; public: void setone(int num){a=num;} int getone(){return a;} }; // derived from base class 'one' class two:public one { int b; public: void settwo(int num){b=num;} int gettwo(){return b;} }; // derived from derived class // 'two' class three:public two { int c; public: void setthree(int num){c=num;} int getthree(){return c;} }; void main(void) { ... More About: Class , Inheritance , Ving
Protected Members of a Class
2007-08-01 15:45:00 In the previous article Defining Base Class Acess (Inheritance ) we noticed that when we derive one class from a base class then no matter which base class access-specifier we use, the members of the derived class doesn’t have access to the private members of the base class. But what if we want certain members of the base class to be private to the class and at the same time accessible to the members of the derived class? Is there any way of doing it? Yes, it is possible, by declaring those members as protected members of the base class. The following example will show you how: // ---------------------------- // -- THIS PROGRAM WON'T RUN -- // ---------------------------- // THIS PROGRAM IS DESIGNED TO // HAVE ERRORS #include<iostream.h> // base class class base { int a; protected: int b; public: int c; }; // 'derived' class is // inheriting 'base' // publicly class derived:public base { in... More About: Prot , Members
Defining Base Class Acess (Inheritance)
2007-08-01 14:58:00 In the previous article Introduction to Inheritance in C++ we saw how one class can inherit properties and functions from another, its general form is: class derived-class:access-specifier base-class { ... ... ... }; We discussed that the access-specifier here, can be anyone of the three private, public and protected. In this article we’ll discuss the meaning of each of these in detail. We know that when a class is derived from another class (base class) then the members of the base class becomes the members of the derived class. The access-specifier used while deriving the class specifies the access status of the base class members inside the derived class. If we don’t use any base class access-specifier then it’s taken to be private by default. Please note that in no case are the private members of the base class be accessible to the members of its derived class. So by using different access-specifier we only chang... More About: Class , Base
Introduction to Inheritance in C++
2007-07-31 08:37:00 Inheritance in C++ is one of the major aspects of Object Oriented Programming (OOP). It is the process by which one object can inherit or acquire the features of another object. In C++ class and structure can use inheritance. It means we can make one class (known as derived class) to acquire or inherit the features of another class (known as base class). Base class has general features common to all the derived classes and derived class (apart from having all the features of the base class) adds specific features. This enables us to form a hierarchy of classes. Ex. if we define a class ‘computer’ then it could serve as the base class for defining other classes such as ‘laptops’, ‘desktops’ etc.. This is because as you know that laptops have all the features of computers and so have desktops (and so should their classes) but they have their specific features too. So, rather than defining all the features of such classe... More About: Introduction , Class , Inheritance , Intro
How String Functions Work? Part II
2007-07-30 08:57:00 This is the second part of the article How String Functions (strinh.h) Work ? Here we'll be designing our own version of some other commonly used standard library string manipulation function that we discussed in the article String Manipulation Functions (string.h) II. These will help increase your programming skills further. mystrlwr(): // mystrlwr function #include<iostream.h> // -- FUNCTION PROTOTYPES -- char *mystrlwr(char *); // -- ENDS -- void main() { char ch[]="C++ ProGramming123"; cout<<mystrlwr(ch); } char *mystrlwr(char *str) { char *temp; temp=str; while(*str!=' ') { // change only if its a // UPPER case character // intelligent enough not to // temper with special // symbols and numbers if(*str>=65 && *str<=90) *str+=32; str++; } return temp;; } mystrupr(): // mystrupr function #include<ios... More About: Part , Pointers , Strings
What is Polymorphism?
2007-07-28 15:37:00 Polymorphism means to have one interface for different methods or functions. It is the ability through which we can do different operations (by calling different functions) from the same interface. In C++ functions and operators can be made to be polymorphic, but in this article we’ll only be discussing about polymorphic functions. There can be two types of polymorphism which are discussed below: Compile-Time Polymorphism: When we have two or more polymorphic function (overloaded functions) and the call to a particular function is resolved (or known) at the compile-time, it is called compile-time polymorphism. The following example program illustrates this. Please read the comments for further information. // program to demonstrate // compile-time polymorphism #include<iostream.h> // -- FUNCTION PROTOTYPES -- // -- OVERLOADED FUNCTIONS -- void func(int); void func(char *); // -- ENDS -- void main() { ... More About: Functions , Pointers , Polymorphism , Poly , Morph
Array of Functions
2007-07-28 08:09:00 In the article Pointers to Function , we saw how pointers can be made to point at functions and hence can be used to invoke them. By far the most important use of pointers to functions is to have arrays of functions. This can be achieved as stated below You already know that we can have arrays of pointers and pointers can be made to point at functions. So combining both we can have array of pointers to functions put differently, we can have array of functions. The example program below demonstrates how we can have array of functions; please note that this concept is mostly used in writing compilers and interpreters, so you shouldn’t expect the program to do anything serious or useful! // Program to demonstrate // array of functions #include<iostream.h> // -- FUNCTION PROTOTYPES -- void func1(); void func2(); void func3(); void func4(); void func5(); // -- ENDS -- void main() { // notice the prototype void (*ptr[5])();... More About: Functions , Arrays , Array
String Manipulation Functions (string.h) II
2007-07-27 15:26:00 This is the continuation of the article String Manipulation Function s (string.h) in which we’re discussing about the string manipulation functions. Here I have listed 8 functions along with their prototype (simplified) and a short description. One thing to note here is that unlike the last article on this topic, I have not included example programs, since the functions (with their prototypes) are pretty much straightforward. strlwr: Prototype: char *strlwr(char *) This function converts the given string to lowercase and returns the same. strupr: Prototype: char *strupr(char *) This function converts the given string to UPPERCASE and returns it. strncat: Prototype: strncat(char *str1, const char *str2, int n) It appends first ‘n’ characters of str2 to the end of str1. strncpy: Prototype: int strncmp(char *str1, const char *str2, int n) This function compares first ‘n’ characters of str1 with str2, it returns 0... More About: Strings
How String Functions (strinh.h) Work?
2007-07-26 15:57:00 In the previous article String Manipulation Functions (string.h), we had a look at some of the commonly used string manipulation functions. There is no denying the fact that those functions are useful but have you ever wondered how those functions actually work or what is the algorithm behind their working? If yes then read on… In this article I am going to present you with our own version of the string manipulation functions that we had discussed, namely strlen(), strcpy(), strcat() and strcmp(). Our versions will do the same thing as done by the original functions but surely they would teach us a lot! Let's have a look at them one-by-one: mystrlen // mystrlen- function #include<iostream.h> int mystrlen(const char *); void main(void) { char ch[]="This is great!"; cout<<"Length:"<<my strlen(ch); } int mystrlen(const char *str) { int len=0; while(str[len]!=' ') len++; return l... More About: Tricks , Work , Methods , Algorithms
String Manipulation Functions (string.h)
2007-07-24 15:33:00 This article discusses about the classic string manipulation functions defined in the string.h header file. From quite a while peoples have been asking me to write an article on the standard library string manipulation functions. These functions are defined in the string.h header file, so you must include it to use them. There are dozens of string functions in the string.h header file and thus it is difficult to list them all. So rather than listing them all I would be discussing in detail about only few commonly used string manipulation functions along with an example program illustrating how each function is used. strlen: Prototype: int strlen(const char *string); This function takes the base address of the string as the argument and returns the number of characters in it (including spaces). // strlen() string manipulation // function #include<iostream.h> #include<string.h> void main(void) { char ch[]="String Manipulation &qu... More About: Functions , Strings , Function
Introduction to Linked Lists III
2007-07-23 15:46:00 In the article Introduction to Linked Lists , we introduced the concept of linked list, the example program was programmed to be able to add and display the elements in the linked list. In reality only addition of elements to the linked list is not enough to take the most out of linked list; we should be able to do other operations such as insertion, deletion of elements etc. This article would teach you to do such operation (insertion, addition, deletion etc). The program itself is quite big and has enough comments so I won’t discuss anything here; rather I leave it up to you to understand all the operations yourself! // -- Linked Lists -- // ------------------ // Example program to illustrate // addition, insertion, deletion // and display of nodes in the // linked list #include<iostream.h> // node class, this will // represent the nodes or elements // of the linked list class node { public: int info; node ... More About: Pointers , Methods , Algorithms
Introduction to Linked Queues
2007-07-23 08:39:00 In one of the article Intro duction to Linked Stacks, I said that representing data structures such as Stacks and Queues as arrays had one major problem that it can?t have more than a predefined number of elements. To overcome this we used linked lists to represent stacks. In this article we?ll use linked lists to represent queues. Below are some graphics that illustrate the addition and retrieval of elements to and from the linked queue. FIG.: Addition of data to the linked queue FIG.: Retrieval of elements from the linked queue I don?t think there is anything more that needs to be discussed, so let?s have a look at the example program: // -- Linked Queues -- // C++ Example Program to // illustrate the representation // of queues as linked lists #include<iostream.h> // node class, this will // represent the nodes or elements // of the linked queues class node { public: int info; node *link; }; // declare global objects node *f... More About: Methods , Algorithms
Increase your Programming Skills III
2007-07-22 07:48:00 Here I have listed some questions related to strings in c++, solve them to increase your programming skills. Problem #1: // Problem related to strings #include<iostream.h> void main(void) { char ch[]="Programming Skills "; int i=0; cout<<ch[++i]; cout<<ch[i++]; cout<<i++[ch]; cout<<++i[ch]; } Problem #2: // Problems related to strings #include<iostream.h> void main(void) { char ch[]="Programming Skills"; char *s="Programming Skills"; cout<<sizeof(ch)<<sizeof(s); cout<<sizeof(*ch)<<sizeof(*s) ; } Problem #3: // Problems related to strings #include<iostream.h> void main(void) { int n[]={4,3,2,1,0}; for(int i=0;i<5;i++) cout<<n[n[i]]; } Problem #4: // Problems related to strings #include<iostream.h> void main(void) { int n[]={11,10,9,8,7,6,5,4,3,2,1,0}; char ch[]="C++ Languag... More About: Questions , Strings
Increase your Programming Skills II
2007-07-21 14:46:00 This is the continuation of the last article? Increase your Programming Skills Solve the problems listed here to gain programming skills. Problem #5: // Problem or Question in C++ // -------------------------- // Problem related to pointers // for increasing programming // skills #include<iostream.h> void main() { char ch1[]="Programming"; char ch2[]="Skills"; char *s1=ch1; char *s2=ch2; while(*s1++=*s2++); cout<<ch1; } Problem #6: // Problem or Question in C++ // -------------------------- // Problem related to pointers // for increasing programming // skills #include<iostream.h> void main() { int i, *j; i=1; j=&i; cout<<i**j*i+*j; } Problem #7: // Problem related to pointers #include<iostream.h> void main() { int ar[]={1,2,3,4,5}; char *p; p=(char *)ar; cout<<*((int *)p+3); } Problem #8: // Problem related to pointers #include<iostr... More About: Questions , Problems , Pointers
Increase your Programming Skills I
2007-07-21 14:42:00 Here I have listed some selected problems or questions related to pointers in C++. Solve them to increase your programming skills. Problem #1: // Problem or Question in C++ // -------------------------- // Problem related to pointers // for increasing programming // skills #include<iostream.h> void main() { char ch[]="I_Like_C++"; cout<<*&*&ch; } Problem #2: // Problem or Question in C++ // -------------------------- // Problem related to pointers // for increasing programming // skills #include<iostream.h> void main() { int i,*p,**q,***r; i=10; p=&i; q=&p; r=&q; cout<<i<<*p<<**q<< ;***r; } Problem #3: // problem related to pointers #include<iostream.h> void main() { char ch[]="Programming Skills "; char *s=ch; cout<<s+++3; } Problem #4: // Problem related to pointers #include<iostream.h> v... More About: Questions , Problems , Pointers
Introduction to Linked Stacks
More articles from this author:2007-07-20 08:47:00 In the article Data Structures: Introduction to Stacks , we saw that there was one major disadvantage of representing stacks using arrays- the stack like the array could have a limited number of elements, while stacks should be able to grow up to any number of elements. Besides this there were other disadvantages too. In one of the other article about Linked Lists, we noticed one useful property of linked lists that they can grow up to any size to accommodate for the addition of elements and it efficiently uses the memory too. So if we combine both of this to from a linked version of the stack then it won’t have the shortcomings that the array version had. This is what this article is all about. pushing and popping As you know that addition of elements to the stack is known as pushing while retrieval is known as popping. The process of pushing and popping in case of linked version of stack is slightly different from the array version. The followin... More About: Methods , Algorithms , Data Structures 1, 2, 3, 4, 5, 6 |



