Showing posts with label OOPS'. Show all posts
Showing posts with label OOPS'. Show all posts

Tuesday, December 7, 2010

Access specifier

In Object Oriented Programming there are three types of access specifier is available. They are:

1.private
2.public
3.protected

syntax:
access_specifer:
member declarations;

private:
private members are private to class,which mean it allows the instance member to access within the class, access beyond that class scope makes error.
private data members are accessed through class public member functions.
In C++ private is default to class it means if we didn't specify any specifier its private to that class

syntax:
private:
member declaration;

Example:
private:
int reg_no;

public:
public members are public to a program/class, which means its like global variable of a program/class, we can access anywhere in the program/class. In java public is default to class it means if we didn't specify any specifier its public to that program/class.

syntax:
public:
member declaration;

Example:
public:
reg_no;

protected:
protected is same as private, we cant able to access the protected variable beyond a class scope. But, when we inherit the protected members of a class, we can able to access those protected member as private to that derived class.

syntax:
protected:
member declartion;

Example:
protected:
char address[40];

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;
}