01-11-2016, 12:11 PM
1462939309-oopslabmanual.docx (Size: 54.74 KB / Downloads: 4)
1. Including header files :- In C++, the #include preprocessor directive causes the compiler to replace that line with the entire text of the contents of the named source file (if included in quotes: "") or named header (if included in angle brackets: < >) note that a header need not be a file.
2. Class declaration:- Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members.
An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.
3. Class function defination:- A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object.
4. Main function program:- The main function is called at program startup after initialization of the non-local objects with static storage duration. It is the designated entry point to a program that is executed in hosted environment (that is, with an operating system). The entry points to freestanding programs (boot loaders, OS kernels, etc) are implementation-defined. The parameters of the two-parameter form of the main function allow arbitrary multibyte character strings to be passed from the execution environment (these are typically known as command line arguments), the pointers
Concept of class declaration
Class declaration:- A class in C++ is a user defined type or data structure declared with keyword class that has data and functions (also called methods) as its members whose access is governed by the three access specifiers private, protected or public (by default access to members of a class is private). A class (declared with keyword class) in C++ differs from a structure (declared with keyword struct) as by default, members are private in a class while they are public in a structure. The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class. Instances of these data types are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer.
Classes and structs are user-defined types, defined by class-specifier, which appears in decl-specifier-seq of the declaration syntax. The class specifier has the following syntax:
OBJECT : (2b) Write a Program for Constructor and Destructor
CONSTRUCTOR
In class-based object-oriented programming, a constructor in a class is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.
It is a special member function for an automatic initialization of an object.
*Constructor name is same as the name of its class in which it is declared.Whenever an object is created, a special member function the constructor executed automatically.
*Constructor is always declared in a public section of the class.
*They cannot be inherited through a derived class.
*It is declared with no return type not even void because they cannot return any value.
TYPES OF CONSTRUCTORS:
1. 1.DEFAULT OR NORMAL CONSTRUCTOR.
2. 2.COPY CONSTRUCTOR.
3. 3.PARAMETERIZED CONSTRUCTOR.
1.DEFAULT OR NORMAL CONSTRUCTOR:-
A default constructor is a constructor that either has no parameters ,or if it has parameters, all the parameters have default value.
If the programmer does not supply a constructor for an insatiable class, most languages will provide a default constructor. The behavior of the default constructor is language dependent. It may initialize data members to zero or other same values, or it may do nothing at all.
2. COPY CONSTRUCTOR:-
Copy constructors define the actions performed by the compiler when copying class objects. A copy constructor has one formal parameter that is the type of the class (the parameter may be a reference to an object). It is used to create a copy of an existing object of the same class. Even though both classes are the same, it counts as a conversion constructor.
Syntax for the copy constructor is:
classname ::classname(classname&pointer)
Whenever we have statement like
classname object1=object2;
To make a call of copy constructor.
3. PARAMETRIZED CONSTRUCTOR:-
Constructors that can take arguments are termed as parameterized constructors. The number of arguments can be greater or equal to one(1).
It may be necessary to initialize the various data elements of different objects with different values when they are created.
This is achieved by passing arguments to the constructor function when the objects are created.
The constructors that can take arguments are called parameterized constructors.
DESTRUCTOR
"Destructor" functions are the inverse of constructor functions. They are called when objects are destroyed (deallocated). Designate a function as a class's destructor by preceding the class name with a tilde (~). For example, the destructor for class
String is declared: ~String ().
Declaring destructors:
Destructors are functions with the same name as the class but preceded by a tilde (~)
The first form of the syntax is used for destructors declared or defined inside a class declaration; the second form is used for destructors defined outside a class declaration.
Several rules govern the declaration of destructors. Destructors:
• Do not accept arguments.
• Cannot specify any return type (including void).
• Cannot return a value using the return statement.
• Cannot be declared as const, volatile, or static. However, they can be invoked for the destruction of objects declared as const, volatile, or static.
• Can be declared as virtual.