Saturday, May 12, 2007

What is Object?



Object is like a variable
as we create with built-in datatypes. Object is an instance of class.
Object has state,behavior and identity.

state - value of data member at any point of time
behaviour - how object performing a task, it comes from definition of member functions.
identity - name of object.

How to create Object?

Syntax:
class-name object-name;
or
class-name object-name1,object-name2,....;

Suppose we have to create object(s) of class Circle :

Circle c; //One object created
or
Circle c1,c2; //Two objects created

How to call a member function?

Syntax :
object-name.member-function-name();

for example we want to set radius of object c , we have to call a member function SetRadius(int) :
->
c.SetRadius(12);
in same way
int area = c.GetArea();

and so on....

Class Members :
Data Member
Member Function
Members must be declared/defined within class definition.
A member can not be declared twice in the class definition.

Now let's assemble all the pieces together to make it meaningful..

//class definition
class A
{
//data member
int i;
int j;

//member function
int getI() //definition of member function
{
return i;
}
void setI(int a) //definition of member function
{
i = a;
}
};
int main()
{

A aObj; //Creation of object
aObj.setI(20); //calling member function
cout<&ltaObj.getI(); //calling member function
}

Please visit us again .Updation is ongoing process here...Thanks

Friday, May 11, 2007

Class & Objects


Class:
It is a basic block of Object Oriented Software.
Class is like a data type as struct in C. A datatype contains data and operations applicable to that data. We can take example of int data type, it contains data as well as operations like a+b , a-b , a*b etc.

In other words, class is a blueprint for objects. class decides what object can have and what task it can perform.

Now we will see, how class concept can be implemented through C++ -

General Form of Class

class
{
data member;

member functions;

};
// ; is must here

Example :

class Circle
{
int radius;

int GetRadius();
void SetRadius(int);
int CalculateArea(int);
};
:

In above example , we have seen how a class look like .
As we know by default, all members of class have private access-specifier. So in above class Circle, all the members will be private,means they can not be accessed from outside the class.Before discussing further, first we have a idea regarding Access Specifier :-

Access-Specifier : also known as visibility labels.
There are three types of Access specifiers

private : private members can be accessed only within the class itself.
public : public members can be accessed from outside the class also.In this case, member is fully exposed to outside-world.
protected : can be access from derived class only.(We will discuss this in detail during inheritance topic)

Now again we take above example. none of the member can be accessed from outside of the class, now to make some member accessible from outside we make one little but very important modification -


class Circle
{
int radius;

public:
int GetRadius();
void SetRadius(int);
int CalculateArea(int);
};
:

Now, to use this definition of class , Let's understand What is Object?

Saturday, May 5, 2007

C++ Structure vs C++ Class

C++ programming language provides structure and class to create problem-oriented datatypes.Instance of these datatypes are known as objects.

C++ Structure and C++ class are exactly same except default access specifier of their members i.e. in C++ Structure all members are public by default while in Class all are private.

For example in following code , the class C is equivalent to structure S

class C
{
//default access specifier is private
int num;

public:
void setNum(int n)
{
Num = n;
}
}

struct S
{
//default access specifier is public
void setNum(int n)
{
Num = n;
}

private:
int num;
}

Hence we can see here that the difference in both is only access specifier, but as security is concern, this is a big drawback of structure.Because by default all the members are exposed to outside world. That is the one main reason, why programmers are hesitant to use structure.In turn, class encourages encapsulation/data-hiding by default.So my recommendation is use class, forget structure.

Thursday, May 3, 2007

C Structure vs C++ Structure

C Structure contains only data items while C++ structure contains data as well as function.

In C to create structure variable you have to use 'struct' keyword for example
struct Person p;

But in C++ 0nly Structure name is used just like built-in datatype. for example
Person p;

Example :

C Structure :
struct Person
{
int pid;
char name[25];
....
}
Declaration of a variable
struct Person p;

C++ Structure :

struct Person
{
int pid;
char name[25];
....
void enterDetails()
{
...
}
..
}

Declaration of a variable:
Person p;

Wednesday, May 2, 2007

Basic Features

Stream : It is a flow of data to and fro.
Standard Input Stream
Used to read data form Standard Input device i.e. keyboard ,it can take input from a file stored at hard disk and from other input devices also.
'cin' represents the Standard Input Stream.
Standard Output Stream
Used to send output to Standard Output Device i.e. Monitor, it can send output to file on hard-disk,printer etc.
'cout' represents the Standard Output Stream.
Standard Error Stream
Another output stream used by programs to output error messages.
'cerr' represents the Standard Error Stream.

The iostream is an object-oriented library that contains input and output streams.

C++ provide two ways to work with streams :
low-level
high-level
Very soon we will get detailed description of this topic...

C++ Comments
Single line comment
// single line statement here
Multiline Comment
/*
Some lines here
*/

References

C++ references are to create alternative name or alias name for the already defined variable.
for example :
int i = 18;
int &ref = i;
here ref is reference to integer variable i. it can be used in place of i.
->& is not address operator here, it is reference operator.<- REMEMBER :

* No memory is allocated for reference variables as they are alias name for simple variables.
* Reference variables should be initialized.

Main Use :
* as a formal argument to a function.
Example :
--------------------------------------
#include
using namespace std;
int Square(int &Val);
int main()
{
int Number=10;
Square(Number);
cout<<"Number is "<<>Pointer Reference:
It is Alias name to the pointer.

Example:
int ival = 100;
int *ptr = &ival;
int * &refptr = ptr;

Scope Resolution Operator :
In C a local variable has precedence over global variable with the same name.
In C++, global variable can be accessed through Scope Resolution Operator :: ,with local variable.
Example:

int i = 4;
int main()
{
int i = 9;
cout<< ::i<< endl; //Prints global variable i 4
cout<< i; //Prints local variable i 9
}
}