Classof1 logo
Fax: 1- 425- 458- 9358 | Toll free: 1- 877- 252 - 7763
Bookmark and Share
Forgot Password? Click Here
Register  |  Account

Need help with Computer Science assignment?

Get customized homework help now!

Classes

Structures:

Structures: A structure is a collection of different types of variables.In structures we can keep only data. It is not possible to keep functions. Data hiding is not possible in structures .To overcome these drawbacks we use classes.

Classes:

A class is a userdefined data type. It contains data and its related functions. The data and functions in a class can be defined by any one of the following visibility modifiers.

  • public
  • private
  • protected

If a data or function defined as public, it can be accessed from outside the class.

If a data or function is defined as private, it cannot be accessed from outside the class. This is called data hiding.

If a data or function is defined as protected it can be accessed only by the classes derived from this class.

Specifying a class:

While specifying a class we have to declare and define its variables and functions.

Class definition:

A class definition contains a keyword class and a user defined class-name followed by data and functions named as members of the class.The members are enclosed with in braces. General form:

Class classname
 {
private:
variable declaration;
function declaration or definition;
public:
variable declaration;
function declaration or definition;
protected:
variable declaration;
function declaration or definition;
};
where class – keyword to define class.
Class name – valid C++ variable.

Rules:

The heads private, public, protected are all optional. If nothing is mentioned, the data and functions are treated as private.

The closing braces should end with semicolon.

If the function is declared inside the class it should be defined outside the class.

All member functions must be defined either inside or outside the class.

The variables declared inside the class are called data members and the functions are called as member functions.

Defining member function outside the class:

If a member function is declared inside the class, it should be defined outside the class. Syntax:

Return data_type class_name:: member_function_name(argument list)
{
local variable declaration
body of the function
}

where,

class_name – name of the defined class

:: - scope resolution operator

member_function_name –  name of the declared function.

Using the class:

If we want to use the defined class in a program.We have to do the following:

  • Declaring class as a variable or creating objects.
  • Accessing member functions.

a) declaring class as a variable or creating objects:

All defined classes should be declared before using it in the program.This is called declaring class as variable. General form:

Class classname
{
body of the class
} object1, object2…….objectn;

where class_name – name of the already defined class

object1….objectn – valid c++ variable name known as objects.

b)Accessing member functions:

To access the member function for an object .(dot) operator is used.

Syntax:

objectname.Memberfunctionname ( )

Where object name – name of already declared object

Memberfunctionname – name of the already defined member function.

The dot operator connects the object and the member function. This is also called class member access operator.

Computer Science Homework Help
Name* :
Email* :
Country* :
Phone* :
Subject* :
Upload Homework :
Upload another homework (upto 5 uploads max.)
Due Date
Time
AM/PM
Timezone
Instructions
(Type Security Code - case sensitive)