0% found this document useful (0 votes)
4K views8 pages

2ND Puc Computer Science Notes PDF

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4K views8 pages

2ND Puc Computer Science Notes PDF

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

2ND PUC COMPUTER SCIENCE NOTES PDF (5 MARKS

QUESTIONS AND ANSWERS)


1. Explain basic concepts of object oriented programming OR describe any five major characteristics of OOP?

Ans: Following are the major characteristics of OOPs


- Objects
- Classes
- Data abstraction
- Data encapsulation
- Inheritance
- Overloading
- Polymorphism
- Dynamic binding
- Message passing
 Objects: An object may be a person, place or a table of data. An object is a collection of data members and function
members.
Ex: Apple, Orange, Mango etc are the objects of the class fruit
Objects take memory and have addresses
Object-1 (student)

 Classes: A class is a group of objects having similar characteristics. Once a class is defined, any
number of objects of that class are created
Ex 1: man and woman belongs to the same class called Human Being.
Ex 2: planets, sun, moon are members of class solar system.

 Data abstraction: Abstraction is a process of representing essential features without including background details or explanations.
 Data encapsulation: data encapsulation combines both data and functions in a single unit called class. Data encapsulation
prevents data from direct access. The data can be accesses only through functions present inside the class definition. Data
encapsulation enables data hiding or information hiding.
Object

Data

Methods

 Inheritance: The concept of inheritance provides the use of reusability. This means that we can add additional features to the
existing class without modifying it. Thus the process of acquiring properties of one class to another is called inheritance.
The existing class is known as base class. and the new class obtained is called derived class.
The derived class shares some of the properties of the base class. Therefore a code from a base
class can be reused by a derived class

 Overloading: There are two types of overloading:


a) Operator overloading: when existing operator operates on new data type is called operator
overloading.
b) Function overloading: Two or more functions have same name but different number of arguments or data type of
arguments. Function overloading is a process of defining same function name to carry similar types of activities with various
data items.

 Polymorphism: it is a process where a function can take multiple forms based on the type of arguments, number of arguments
and data type of return value
The ability of an operator and function to take multiple forms is known as polymorphism..
Ex: int addition(int a, int b);
int addition( float a, float b);

 Dynamic binding: binding is a process of connecting one program to another. Dynamic binding is a process of connecting one
program to another during execution (when function is called).
 Message passing: a message for an object is request for execution of procedure. Message passing involves specifying the name
of object, the name of the function and the information to be sent.

2. What are the advantages of OOP over earlier programming methods


Ans: Following are the advantages of OOP over earlier programming methods
- Using class and objects, programs are modularized
- Linking code and object allows related objects to share common code. This reduces code
duplication and code reusability
- As the data is encapsulated along with functions, the non-member functions cannot access or
modify data. Thus, providing data security
- Complexity of the program development is reduced through the use of inheritance
- Reduces time, as creation and implementation of OOP code is easy
- Through message passing OOP communicates to the outside system.

3. What are the application of OOP.


Ans: Applications of OOP are as follows
- Computer graphic applications
- CAD/CAM software
- Object oriented database
- User interface design such as windows
- Real-time systems
- Simulation and modeling
- Artificial intelligence and expert systems

4. Explain class definition and class declaration with syntax and example
Ans: A class definition is a process of naming a class, data and functions of the class
A class declaration specifies, representing objects and functions that operate on objects.
The definition and declaration of a class includes the following:
- The data members of the class describes the characteristics of the class
- The function members are the set of operations that are performed on the objects of the class.
- The access control specifiers (private, protected and public) to the class members within a
program are specified
- Class name is used for external operations for accessing and manipulating the instance of a class.

Syntax of a class:
class class_name
{
private: data member;
function member;
protected: data member;
function member;
public: data member;
function member;
};
Example:
class student
{
private: int rollno;
char name[15];
float percentage;
public: void getdata();
void putdata();
};

5. Write a program to demonstrate the use of class and object


Ans: #include<iostream.h>
#include<conio.h>
class demo
{
private: int a,b;
public: void getdata();
{
cout<<”Enter the value of A: ”;
cin>>a;
cout<<”Enter the value of B: ”;
cin>>b;
}
void putdata()
{
cout<<”Addition of two numbers=”<<a+b;
}
};
void main()
{
demo d;
d.getdata();
d.putdata();
getch();
}
6. Explain how to write member functions written inside and outside the class definition
Ans: To define member function inside a class the function declaration within the class is replaced
by actual function definition inside the class. Only small functions are defined inside class definition
Ex:
class room
{
int length;
int breadth;
public: void getdata()
{
cin>>length;
cin>>breadth;
}
void putdata()
{
cout<<length;
cout<<breadth;
}
};
To define member functions written outside the class definition replaced by actual function definition
outside the class.
Ex:
class room
{
int length;
int breadth;
public: void getdata();
void putdata();
};
void room::getdata()
{
cin>>length;
cin>>breadth;
}
void room::putdata()
{
cout<<length;
cout<<breadth;
}
The symbol :: is known as scope resolution operator. The scope resolution operator identifies the
function as a member of particular class. The use of scope resolution operator implies that these
member functions are defined outside the class.

7. Explain how objects of a class can be defined?


Ans: when a class is defined, it specifies that the user defined data type is defined. The objects of a class
are declared in the same manner like any other variable declaration
Syntax:
class class_name
{
private members;
public members;
};
class_name object1, object2…;

Ex:
class num
{
private: int x;
int y;
public: int sum(int a, int b);
int diff(int a, int b);
};
void main()
{
num n1,n2;
n1.sum(10,5);
n2.diff(10,5);
}
Note that an object is an instance of a class template.

8. Explain how an array of objects can be defined?


Ans: An array having class type elements is known as array of objects. An array of objects is
declared after class definition and is defined in the same way as any other array
Ex:
class employee
{
char name[15];
int age;
float salary;
public:
void getdata()
{
cout<<”Enter the name of employee: ”;
cin>>name;
cout<<”Enter the age of an employee: ”;
cin>>age;
cout<<”Enter the salary: ”;
cin>>salary;
}
void putdata()
{
cout<<”Name: ”<<name;
cout<<”Age: ”<<age;
cout<<”Salary: ”<<salary;
}
};
void main()
{
employee emp[3];
for(int i=0;i<3;i++)
{
emp.getdata();
emp.putdata();
getch();
}

9. Describe the need for function overloading


Ans: function overloading means two or more functions have same name, but differ in the number
of arguments or data type of arguments. Therefore, it is said that function name is overloaded. The
advantages of overloaded functions are:
- When functions are overloaded, then the compiler automatically decides and executes appropriate
function based on type of arguments and calls the required function. Thus the code is executed faster.

- It is easier to understand the flow of information and debug.


- Code maintenance is easy.
- Easier interface between programs and real world objects.

10. Explain overloaded functions with syntax and example


Ans: function overloading means two or more functions have same name, but differ in the number
of arguments or data type of arguments. Therefore, it is said that function name is overloaded.
Syntax:
return_type1 function_name(argument list)
{
statement(s);
}
return_type2 function_name(argument list)
{
statement(s);
}

return_typen function_name(argument list)
{
Statement(s);
}
Ex:
int sum(int a, int b)
{
return(a+b);
}
float sum(float a, float b)
{
return(a+b);
}

11. Explain inline function with syntax and example


Ans: In inline function compiler replaces a function call along with the body of the function. Inline
function run little faster than normal functions as function calling overheads are saved. Advantages of
inline function are as follows:
- They are compact function calls
- The size of the object code is reduced
- Very efficient code can be generated
- The readability of the program increases
Syntax:
inline retrun_type function_name(argument list)
{
statement(s);
}
Ex:
#include<iostream.h>
#include<conio.h>
inline int square(int a)
{
return(a*a);
}
void main()
{
int x;
x=square (5);
cout<<”Square of 5=”<<x;
getch();
}

12. Explain friend function and their characteristics. Or explain friend function with syntax and
programming example.
Ans: A friend function is a non member function that is a friend of a class. The friend function is declared
within a class with the prefix friend. But it should be defined outside the class like a normal function
without the prefix friend.
Syntax:
class class_name
{
public: friend void function(void);
};
Characteristics of a friend function are as follows:
- A friend function has full access right to the private and protected members of the class.
- A friend function cannot be called using the object of that class. It can be invoked like any normal
function.
- A friend function can be declared anywhere in the class and it is not affected by access specifiers
(private, protected and public).
- They are normal external functions given special access privileges.
- The function is declared with keyword friend. But while defining friend function it does not use either
friend or :: operator.
#include<iostream.h>
#include<conio.h>
class myclass
{
int a,b;
public: void set_val(int i, int j );
friend int add(myclass obj);
};
void myclass::set_val(int i, int j)
{
a=i;
b=j;
}
int add(myclass obj)
{
return(obj.a+obj.b);
}
void main()
{
myclass object;
object.set_val(10,20);
cout<<”sum =”<<add(object);
getch();
}

13. What are the rules for writing a constructor function?


Ans: Rules for writing a constructor function are as follows
- A constructor name is always same as that of the class name.
- There is no return type for the constructors not even void.
- A constructor should be declared in public section.
- A constructor is invoked automatically when objects are created.
- It is not possible to refer to the address of constructors.
- The constructors make implicit calls to the operators new and delete when memory allocation is
required.

14. Explain default constructor with syntax and example


Ans: A constructor which does not accept any arguments is called default constructor. Default constructor
simply allocated memory to data members of objects. Features of default constructors are
- Default constructor automatically invokes when object is created.
- All objects of the class are to be initialized to same set of values by the default constructor.
- If different objects are to be initialized with different values, it cannot be done using default
constructor.
Syntax:
class_name calss_name()
{
}
Ex:
#include<iostream.h>
#include<conio.h>
class c
{
int a,b;
public: c()
{
a=10;
b=20;
}
void display()
{
cout<<a<<b;
}
};
void main()
{
c obj;
obj.display();
getch();
}

You might also like