CS1204 - Object Oriented Programming
CS1204 - Object Oriented Programming
Machine language
↓
Procedure language
↓
Assembly language
↓
OOPS
Main program
Function-4 Function-5
8)What is a class?
The entire set of data and code of an object can be made a user-defined data type
with the help of a class. Once a class has been defined, we can create any number of
objects belonging to the classes.
Classes are user-defined data types and behave like built-in types of the
programming language.
9) what is encapsulation?
Wrapping up of data and function within the structure is called as encapsulation.
output:
20
10 (: : m access global m)
Scope resolution operator is used to define the function outside the class.
Syntax:
Return type <class name> : : <function name>
Eg:
Void x : : getdata()
24) What are free store operators (or) Memory management operators?
New and Delete operators are called as free store operators since they allocate the
memory dynamically.
New operator can be used to create objects of any data type.
Initialization of the memory using new operator can be done. This can be done as,
Pointer-variable = new data-type(value)
Delete operator is used to release the memory space for reuse. The general form of
its use is
Delete pointer-variable;
25) What are manipulators?
Eg:
float total = 100;
float sum = total;
30)What is member-dereferencing operator?
C++ permits to access the class members through pointers. It
provides three pointer-to-member operators for this purpose,
The function prototype describes function interface to the compiler by giving details
such as number ,type of arguments and type of return values
33) Write some situations where inline expansion may not work
Function call is
Value=amount(5000,7);
Here it takes principle=5000& period=7
And default value for rate=0.15
Value=amount(5000,7,0.34)
Passes an explicit value 0f 0.34 to rate
We must add default value from right to left
{
m=0;n=0;
}
the statement
integer a;
invokes the default constructor
53) What is the ambiguity between default constructor and default argument
constructor ?
The default argument constructor can be called with either one argument or no
arguments. when called with no arguments ,it becomes a default constructor. When both
these forms are used in a class ,it cause ambiguity for a statement such as A a;
The ambiguity is whether to call A::A() or A::A(int i=0)
inline function-header
{
function body
}
//Function calls
add (3 , 4); //uses prototype ( i. )
add (3, 4, 5); //uses prototype ( ii. )
add (3 , 10.0); //uses prototype ( iii. )
72) What is the purpose of using operator function? Write its syntax.
To define an additional task to an operator, we must specify what it means in
relation to the class to which the operator is applied. This is done by Operator function ,
which describes the task. Operator functions are either member functions or friend
functions. The general form is
return type classname :: operator (op-arglist )
{
function body
}
where return type is the type of value returned by specified operation.
Op-operator being overloaded. The op is preceded by a keyword operator. operator op is
the function name.
74) How will you overload Unary & Binary operator using member functions?
When unary operators are overloaded using member functions it takes no explicit
arguments and return no explicit values.
When binary operators are overloaded using member functions, it takes one
explicit argument. Also the left hand side operand must be an object of the relevant class.
75) How will you overload Unary and Binary operator using Friend functions?
When unary operators are overloaded using friend function, it takes one reference
argument (object of the relevant class)
When binary operators are overloaded using friend function, it takes two explicit
arguments.
78) List out the operators that cannot be overloaded using Friend function.
Assignment operator =
Function call operator ( )
Subscripting operator [ ]
Class member access operator →
79) What is meant by casting operator and write the general form of overloaded casting operator.
A casting operator is a function that satisfies the following conditions
It must be a class member.
It must not specify a return type.
It must not have any arguments.
The general form of overloaded casting operator is
operator type name ( )
{
……….. // function statements
}
It is also known as conversion function.
Base class A
Derived class B
Here class A is the base class from which the class D is derived. Class D is the public
derivation of class B hence it inherits all the public members of B. But D cannot access
private members of B.
Derived class C
Here class C is derived from two base classes A & B.
Intermediate B
Base class Father
Multilevel Student
inheritance
Multiple
Test Sports inheritance
Result
The class result will have both the multilevel and multiple inheritances.
89) What is meant by Abstract base class?
A class that serves only as a base class from which derived classes are derived. No
objects of an abstract base class are created. A base class that contains pure virtual
function is an abstract base class.
Test Sports
Result
91)Define Polymorphism?
Polymorphism is the feature that allows one interface to be used for a general
class of actions.(ie) “one interface multiple methods”.
This means that it is possible to design a generic interface to a group of related
activites.This helps reduce complexity by allowing the same interface to be used to
specify a general class of action.
99)Here var-name is set to null. To link with an actual, physical array of integers,
allocate using new.
bb: array-var=new type[size];
2.it acts like this, except that it always refers to the super class of the subclass in
which it is used.
1.What are the Features of Oop’s & how are they implemented in C++?
• Objects.
• Classes.
• Data abstraction and Encapsulation.
• Inheritance.
• Polymorphism.
• Dynamic binding.
• Message passing.
Function overloading means we can use the same function name to create
functions that perform a variety of different tasks.
Eg: An overloaded add ( ) function handles different data types as shown below.
// Declarations
iv. int add( int a, int b); //add function with 2 arguments of same type
v. int add( int a, int b, int c); //add function with 3 arguments of same type
vi. double add( int p, double q); //add function with 2 arguments of
different type
//Function calls
i. add (3 , 4);
ii. add (3, 4, 5);
iii. add (3 , 10.0);
C++ has the ability to provide the operators with a special meaning for a data type.
This mechanism of giving such special meanings to an operator is known as Operator
overloading. It provides a flexible option for the creation of new definitions for C++
operators.
The operators that cannot be overloaded are.
• Class member access operator (. , .*)
• Scope resolution operator (::)
• Size operator ( size of )
• Conditional operator (?:)
The purpose of using operator function is to define an additional task to an operator, we must
specify what it means in relation to the class to which the operator is applied. This is done by Operator
function , which describes the task. Operator functions are either member functions or friend functions.
The general form is
return type classname :: operator (op-arglist )
{
function body
}
where return type is the type of value returned by specified operation.
Op-operator being overloaded. The op is preceded by a keyword operator. operator op is
the function name. The rules for Operator overloading are
• Only the existing operators can be overloaded.
• The overloaded operator must have at least one operand that is of user
defined data type.
• The basic meaning of the operator should not be changed.
• Overloaded operators follow the syntax rules of the original operators.
They cannot be overridden.
When unary operators are overloaded using member functions it takes no explicit
arguments and return no explicit values.
When binary operators are overloaded using member functions, it takes one
explicit argument. Also the left hand side operand must be an object of the relevant class.
When unary operators are overloaded using friend function, it takes one reference
argument (object of the relevant class)
When binary operators are overloaded using friend function, it takes two explicit
arguments. The operator can be invoked using member functions as follows
In case of Unary operators, overloaded operator can be invoked as
op object_name or object_name op
In case of binary operators, it would be invoked as
Object . operator op(y)
where op is the overloaded operator and y is the argument.
The overloaded operator can be invoked using Friend function as
In case of unary operators, overloaded operator can be invoked as
Operator op (x);
In case of binary operators, overloaded operator can be invoked as
Operator op (x, y)
The operators that cannot be overloaded using Friend function.
• Assignment operator =
• Function call operator ( )
• Subscripting operator [ ]
• Class member access operator →
Inheritance is the process by which objects of one class acquire the properties of
another class. It supports the concept of hierarchical classification. It provides the idea of
reusability. We can add additional features to an existing class without modifying it by
deriving a new class from it.
i. single inheritance
If a single class is derived from a single base class is called single inheritance.
Eg:
Base class A
Derived class B
Here class A is the base class from which the class D is derived. Class D is the
public derivation of class B hence it inherits all the public members of B. But D cannot
access private members of B.
ii. multiple inheritance
If a class is derived from more than one base class, it is called multiple inheritance.
Derived class C
Here class C is derived from two base classes A & B.
Student
Intermediate B
Base class Father
v. Hybrid inheritance
It is the combination of one or more types of inheritance. The class result will
have both the multilevel and multiple inheritances.
Student
Multilevel
Test Sports
inheritance
Multiple
inheritance
Result
8. Define constructor
A constructor is a special member function whose task is to initialize the objects of
its class. It is special because its name is same as class name. The constructor is invoked
whenever an object of its associated class is created. It is called constructor because it
constructs the values of data members of the class
Eg:
Class integer
{
……
public:
integer( );//constructor
………
}
The different types of constructor are
i. default constructor
The constructor with no arguments is called default constructor
Eg:
Class integer
{
int m,n;
Public:
Integer( );
…….
};
integer::integer( )//default constructor
{
m=0;n=0;
}
the statement
integer a;
invokes the default constructor
v.Dynamic constructor
Allocation of memory to objects at time of their construction is known as dynamic
constructor. The memory is allocated with the help of the NEW operator
Eg:
Class string
{
char *name;
int length;
public:
string( )
{
length=0;
name=new char[length +1];
}
void main( )
{
string name1(“Louis”),name3(Lagrange);
}
The class that has different types of constructor is called multiple constructors
Eg:
#include<iostream.h>
#include<conio.h>
class integer
{
int m,n;
public:
integer( ) //default constructor
{
m=0;n=0;
}
integer(int a,int b) //parameterized constructor
{
m=a; n=b;
}
integer(&i) //copy constructor
{
m=i.m;
n=i.n;
}
void main()
{
integer i1; //invokes default constructor
integer i2(45,67);//invokes parameterized constructor
integer i3(i2); //invokes copy constructor
}
The special characteristics of constructor are
• They should be declared in the public section
• They are invoked automatically when the objects are created
• They do not have return types, not even void and therefore, and they cannot
return values
• They cannot be inherited, though a derived class can call the base class
• They can have default arguments
• Constructors cannot be virtual function
• Documentation section
• Package statement
• Import statements
• Interface statements
• Class definitions
• Main method class
• Defining a subclass
• Subclass constructor
• Single Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
Eg:
Class A
{
---------
---------
}
class B extends A
{
---------
---------
}
class C extends B
{
-------
--------
}
Java does not support multiple inheritances. It is achieved using interfaces in Java.
• Defining Interfaces
• Extending Interfaces
• Implementing Interfaces
• Accessing Interface variables
Eg :
Interface Area
{
final static flaot pi=3.14f;
float compute(float x,float y);
}
• Newborn state
• Runnable state
• Running state
• Blocked state
• Dead state
• Exceptions
• Syntax of Exceptions handling state
• Multiple catch statements
• Using finally statement
Eg:
Class A
{
public static void main (String args[])
{
int a=10;
int b=5;
int c=5;
int x,y;
try
{
x=a/(b-c);
}
catch(ArithmeticException e)
{
System.out.println(“Division by zero”);
}
y=a/b+c;
System.out.println(“y=”+y);
}
}
• Initialization state
• Running state
• Idle state or stopped state
• Dead state
• Display state
• Writing Applets
Eg:
Import java.awt.*;
Import java.applet.*;
Public class A extends Applet
{
public void paint( Graphics g)
{
g.drawString(“Hello”,10,100);
}
}
• Building Applet code
• Designing a web page
<html>
<title>Applet</title>
<body>
<applet code= A.class width=400 height=200 </applet>
</body>
</html>
• Running the Applet
• Using appletviewer
• Using web browser
• Public access
• Friendly access
• Protected access
• Private access
• Private protected access
Methods that have same name but different parameter lists and different
definitions is called Method overloading.
Eg:
class Room
{
int width;
int length;
Room(int x,int y) // Constructor
{
length= x;
width = y;
}
Room(int x)
{
length=breadth=x;
}
}
When a method is called the method defined in the subclass is invoked and
executed instead of the one in the superclass. This is called overriding.
Eg:
class superclass
{
void methodsuper()
{
System.out.println("superclass method");
}
}
class subclass extends superclass
{
void methodsuper()
{
System.out.println("overriding method of superclass");
}
}
class override
{
public static void main(String args[])
{
subclass sb=new subclass();
sb.methodsuper();
}
}
• Arithmetic operators
• Integer arithmetic
• Real arithmetic
• Mixed mode arithmetic
• Relational operators
• Logical operators
• Assignment operators
• Increment and Decrement operators
• Conditional Operator
• Bitwise operator
• Special operators
• Instanceof operator
• Dot operator
• Class declaration
• Opening brace
• Main Line
• Output line
Eg:
class sample // Class declaration
{ // Opening brace
public static void main(String args[]) // Main Line
{
System.out.println(“Hello”); // Output line
}
}