--%>

Class and Object and explain diverse specifies.

Q. Define class and object with example and explain diverse specifies.

 

                                                                          OR  

 

 Describe the class differentiate the public and private members of the class. Also describe the method to access the private and public members of the class.

Ans. Classes: A class is a group of objects that share general characteristics and relationship. It presents a group of identical objects. The complete set of data and code of an object can be made a user defined data type with the aid of a class. Objects are variables of the category class. In other words objects consists of data, and code to change the data. The complete set of data and code of an object can be made of a user defined data type with the aid of a class. Actually, objects are variables of the category class. Once a class has been stated and clear, we can develop any number of objects related to that class. Each object is related with the data of type class with which they are developed. A class is therefore a group of objects of identical type. For instance, apple, mango and orange are parts of the class fruit. Classes are user defined data types and act like the built in tyres of programming language. The syntax utilized to develop an object is no distinct than the syntax used to develop an integer object in C. If fruit has been defined as a class, then the statement

Fruit mango;

Will create an object mango belonging to the class fruit.

    A simple class example

A typical class declaration would look like:

Class item

{

Int number;                 // variables declarations

Float cost;                  // private by default

Public:

Void get data (Int a, float b);              // functions declarations

Void put data (void);                             // using prototype

                                                              // ends with semicolon

};

Distinction between public and private: The differences are given in the tabulated form:

Members of base class (visibility)

members

Of derived

class

(visibility)

 

public derivation

private derivation

Protected derivation

private

Not inherited

Not

inherited

 

Not

inherited

 

protected

protected

Private

protected

Public

Public

private

protected

Accessing private members:

 . Private data type only:-

include < iostream.h >

Class item

{

Int number;                                                   // private by default

Float cost;        // private by default

Public:

Void get data (Int a, float b);             // prototype declaration,

                                                               // to be defined

// function defined inside class

Void put data (void)

{

         Cout << "number:" << number << "\n";

}        cout << "cost :" << cost << "\n";

}; 

    //..............................member function definition.....................................................

Void item :: get data (Int a, float b)                       // use membership label

{  

 Number = a; // private variables

Cost = b;     // directly used

}

//.............................main programme...................................

Int main 0

{

Int main 0

{

Item x;

Cout << "\n object x" << "\n";

X get data (100299.95);                                            // call member function

X. put data 0; call                                              // member function 

Item y;                                                                     // create another object

Cout << "\n object y" << "\n";

y. get data (200, 175.50);

y. put data 0;

Return 0;

}

The object of the programme would be:

Object x

Number: 100

Cost : 299.95

Object y

Number: 200

Cost : 175.5

Prime member function: A private member function can only be called as an added function that is a part of its class. Even an object cannot appeal to a private function with the use of the dot operator. Assume a class as defined below:

Class sample

{

Int m;

Void real (void);                                              // private member function

Public :

  Void up date (void);

Void write (void);

If s 1 is an object of sample, then

S 1.read ();           // would not work; objects cannot access

                             // private members

is illegal. Though the function read 0 can be called by the function up date 0 to update an object to update the value of m.

Void sample:: update (void)

{   

Read 0;                                     // simple call; no object is used

}

   Related Questions in Programming Languages

  • Q : Define CORBA Define CORBA ? What does

    Define CORBA? What does it do?

  • Q : What is Dotted decimal notation Dotted

    Dotted decimal notation: The notation employed to symbolize the 4-byte values of an IP address. Each and every byte is symbolized as a value between 0 to 255, for instance 129.12.0.1. The most noteworthy byte is written at first.

  • Q : Explain Mutator method Mutator method :

    Mutator method: It is a method specifically designed to permit controlled modification of a private attribute of a class. By convention, we name the mutators with a set prefix obeyed by the name of the attribute being transformed. For example, the mut

  • Q : What is an Assembly language Assembly

    Assembly language: This is a symbolic language closely analogous to the instruction set of a Central Processing Unit. The program employed to translate a program written in assembly language is termed an assembler.

  • Q : Different file types Specify the

    Specify the different file types?

  • Q : What is an Object Object : It is an

    Object: It is an instance of a particular class. In common, any number of objects might be constructed from a class definition. The class to which an object belongs states the common characteristics of all instances of that class. In those characteris

  • Q : Procedural and object-oriented programs

    Illustrate the difference between the procedural and object-oriented programs in brief.

  • Q : Reading Algorithms Assignment 1:

    Assignment 1: Algorithms Rules: See the Assignment Rules file on the class Moodle site. 1 Reading Algorithms (20 points) Input: A nonempty string of characters S1S2 . . . Sn, and a positive integer n giving the number of characters in the string. Output: See t

  • Q : What do you mean by Class variable What

    What do you mean by Class variable: It is a synonym for the static variable.

  • Q : Define Preempt Preempt: It is the

    Preempt: It is the presently executing thread might be preempted, or forced to give up control, by a higher priority thread which becomes eligible to run throughout its time slice.