Tuesday, December 7, 2010

Accessing class members

Accessing class members:
class members are accessed within the class.
class members are accessed through class member function.
instance of a class is needed to access class public/private members, outside of its class scope.


Note: private members of a class cannot able to access outside of the class scope. To know more refer access specifiers.


syntax for accessing class members through object:


a dot operator(.) is used to access the class members through instance of a class.

object_name.member_of_a_class;


Example:
S.name='Raja';
here 'S' is object for the class Student and 'name' is instance variable of that class.

Here is a small example to demonstrate accessing class members.

class Student
{
char name[30];
void get_details;
void display_details;
private:
int reg_no;
protected:
char address[40];
}
class Student_details extends Student //inheritance
{
 address="Saveetha Engg College, Chennai.";//derived can access the protected member of a class.*/
reg_no=1042;/*generates error, because scope of the private member is within the class. Not outside of a class or derived class*/
}
class Main
{
public static void main(String args[])
{
Student S=new Student();
S.get_detials();
S.display_details();
S.name="Ragu";
S.reg_no;/*this line generates error. Because we are allowed to access protected member only inside the scope and derived class*/
S.address;/*this line also generates error. Because we are allowed to access protected member only inside the scope*/
}
}

Introduction to Object

Object:

object is the 'instance of the class'.
object contains all the details of the class.
object allocates memory to the class non-static variables.
object has physical reality,i.e., an object occupies physical space in memory.

syntax:
1. class_name object_name;

2. class_name=new class_name();//class_name() is made a call to its class default constructor.

In C++ 1.will create instance for a class, 2. allocates memory statically i.e., at run time in. In JAVA 1. will be reference and the 2. will be instance for a class.

Example:
Student S=new Student();

Introduction to Class

Class:
class is a real world entity.
class contains its own data members and function members.
class is a data type, we can create a variable of a class type.
class is a instance of variables.
class is a logical construct.

syntax:
class class_name
{
access specifier:
datamembers declaratrions;
function member declaarations;
class body;
} object list;

The above syntax is for c++

for java the syntax is as follows:

syntax:
class class_name
{
access specifier:
datamembers declaratrions;
function member declaarations;
class body;
}

the syntax diffecence between c++ and java is semicolon(;) is not expected at the end of the class, while in c++ it is expected. Because in c++ the structure of the class is similar to that of 'Structres'. Here 'class' is a keyword.

Example in java:
class Student
{
char name[30];
int reg_no;
void get_details;
void display_details;
}

Thursday, September 23, 2010

Constructors:

Constructors are similar to functions which has function identifier similar to class identifier. It gets executed when a object came into existence,i.e., when a object is created. There are variety of constructors. They are:

1.Default constructors
2.Parametrized constructors
3.Copy constructors
4.Overloading constructors

Default constructors:
        Default constructor are constructors which has no parameters. Default constructor assign default value as 0 to its public, private and protected data members.

Syntax:
class class_name
{
//class definitions

class_name()
{}
}

Example:
class book
{
int a,b;
public:
book()
{
}
};
int main()
{
book b;
}

Wednesday, September 22, 2010

Inheritance:

Base class functions and members can be redefined in derived class check it out:

Program:
#include<iostream>
class A
{
protected:
int a,b;
public:
void assign()
{
a=30,b=20;
}
void display()
{
cout<<"\na= "<<" b="<<b;
}
};
class B : public A
{
protected:
int c,d;
public:
void assign()
{
a=40,b=50,c=100,d=200;
}
void display1()
{
cout<<"\na="<<a<<" b="<<b<<" c="<<c<<" d="<<d;
}
};
void main()
{
B deriv;
deriv.assign();
deriv.display1();
A base;
base.assign();
base.display();
}

output:a=40 b=50 c=100 d=200
a=30 b=20

Tuesday, September 21, 2010

Operator Precedence in C.

Operator precedence plays a very important role while evaluating a expression and logical output predict in a program. Here i have listed the precendence of operator. So when a expression or logical condition is evaluated, the evaluation takes place in the order of precedence.



Operator Symbol
Name of the operator
!
Logical Not
*,/,%
Arithmetic and modulo
<,>,<=,>=
Relational
==,!=
Relational
&&,||
Logical AND and OR
=
Assignment

Simple java program:

Let's start our java with simple Hello world program.

class sample {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}
Output:
Hello World!