Archive

Posts Tagged ‘c++ programming’

C++ Tutorial 4 – More on Namespaces

July 10th, 2010 Rishabh Dev No comments

To see an introduction to what namespaces are, go back to tutorial 3. Now, cout is a function in the namespace “std”. Now, there’s another way of using cout without specifying the namespace before hand.

#include<iostream>
int main(
{

a
std::cout<<"Hello Jolly!";
system("pause");
return 0;
}

Here, std::cout means the cout that comes from the std namespace. However, since we would eventually use a large number of output and input statements(ie cout’s and cin’s), we would usually declare the namespace beforehand.

Creating our own namespaces

We can also create our own namespaces. These may contain a number of variables and functions to be used for the particular namespace. We here create a namespace called “my” with a variable of integer type called x.

We will be going into variables and data types in a few tutorials, but here’s ow you create an integer variable called x-

int x;   //creates a variable called x
           //which takes integral values

So, here’s our own namespace-

namespace my{   //creating a namespace
int x;
}
int main()
{
my::x;         //using the namespace variable
}

If we intend to use our variable x a number of times, we could specify our namespace in the program.

using namespace my;  //declare namespace beforehand

3 people like this post.

Popularity: 5% [?]

Understanding Inheritance in OOP

October 4th, 2009 Rishabh Dev No comments

The example below explains inheritance, an important property of OOP languages.

We have three classes: living, animal & dog. The dog inherits all the characteristics of living & animal base classes however, the plant does not(being an instance of just the living class).

#include<iostream>
using namespace std;
class living
{
      int energy;
      public:
                void getenergy()
                {
                     cout<<"Gets energy"<<endl;
                     }
};
class animal:public living
{
      int feet;
      public:
                void move()
                {
                     cout<<"It moves"<<endl;
                     }
};
class dog:public animal
{ 

      int tail;
      public:
                void bark()
                {
                     cout<<"It barks"<<endl;
                     }
};
int main()
{
    living plant;
    plant.getenergy(); 

    dog phoenix;
    phoenix.getenergy();
    phoenix.move();
    phoenix.bark(); 

    system("pause");
    return 0;
}

The dog inherits the properties of animal which inherits the properties of living. Hence, this inheritance is an example of “Multi-level” inheritance.

7 people like this post.

Popularity: 2% [?]

10 Major Differences Between C And C++

August 14th, 2009 Rishabh Dev 7 comments

C++, as the name suggests is a superset of C. As a matter of fact, C++ can run most of C code while C cannot run C++ code. Here are the 10 major differences between C++ & C…

1. C follows the procedural programming paradigm while C++ is a multi-paradigm language(procedural as well as object oriented)

In case of C, importance is given to the steps or procedure of the program while C++ focuses on the data rather than the process.
Also, it is easier to implement/edit the code in case of C++ for the same reason.

2. In case of C, the data is not secured while the data is secured(hidden) in C++

This difference is due to specific OOP features like Data Hiding which are not present in C.

3. C is a low-level language while C++ is a middle-level language

C is regarded as a low-level language(difficult interpretation & less user friendly) while C++ has features of both low-level(concentration on whats going on in the machine hardware) & high-level languages(concentration on the program itself) & hence is regarded as a middle-level language.

4. C uses the top-down approach while C++ uses the bottom-up approach

In case of C, the program is formulated step by step, each step is processed into detail while in C++, the base elements are first formulated which then are linked together to give rise to larger systems.

5. C is function-driven while C++ is object-driven

Functions are the building blocks of a C program while objects are building blocks of a C++ program.




Your Ad Here

6. C++ supports function overloading while C does not

Overloading means two functions having the same name in the same program. This can be done only in C++ with the help of Polymorphism(an OOP feature)

7. We can use functions inside structures in C++ but not in C.

In case of C++, functions can be used inside a structure while structures cannot contain functions in C.

8. The NAMESPACE feature in C++ is absent in case of C

C++ uses NAMESPACE which avoid name collisions. For instance, two students enrolled in the same university cannot have the same roll number while two students in different universities might have the same roll number. The universities are two different namespace & hence contain the same roll number(identifier) but the same university(one namespace) cannot have two students with the same roll number(identifier)

9. The standard input & output functions differ in the two languages

C uses scanf & printf while C++ uses cin>> & cout<< as their respective input & output functions

10. C++ allows the use of reference variables while C does not

Reference variables allow two variable names to point to the same memory location. We cannot use these variables in C programming.

80 people like this post.

Popularity: 100% [?]

C++ Tutorial 2 – Multi-Paradigm Programming

August 7th, 2009 Rishabh Dev 1 comment

Whenever the programmers feel the need to change the basic layout of setting up a program, a new paradigm is introduced. Computer languages can then be classified based on the respective paradigm.

A programming paradigm is nothing but a style of programming. In one paradigm we may concentrate on the logic, in another, we may stress on the structure or procedure of our program.

C++ is a programming language that uses three such paradigms & hence, is said to be a Multi-Paradigm programming language.

A) Generic – We generalize concepts as templates & reuse them in the source code.

B) Imperative – We work with a sequence of commands so as to change the state of the program.

(Procedural Programming is the most common way this can be done. Structural Programming is a subset of Procedural programming)

C) Object-Oriented – We use classes which are blueprints of objects that share common behavior & properties.

(The focus here is on the data & instructions rather than the process).

[Go to Tutorial 3]

6 people like this post.

Popularity: 4% [?]

Content Protected Using Blog Protector By: PcDrome.