0% found this document useful (0 votes)
92 views

OOP Chapter 1

This document summarizes Chapter 1 of an Object Oriented Programming textbook. It covers the key differences between procedural and object oriented programming, basic OOP concepts like classes and objects, and provides examples of C++ code demonstrating classes and objects. It also lists common OOP terms and concepts like data abstraction, encapsulation, inheritance and polymorphism.
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)
92 views

OOP Chapter 1

This document summarizes Chapter 1 of an Object Oriented Programming textbook. It covers the key differences between procedural and object oriented programming, basic OOP concepts like classes and objects, and provides examples of C++ code demonstrating classes and objects. It also lists common OOP terms and concepts like data abstraction, encapsulation, inheritance and polymorphism.
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/ 11

OOP Chapter 1 Ali Karim Sir 9220990566

Chapter 1.
Principles of Object Oriented Programming
12mks
Syllabus:
1.1 Procedure Oriented Programming (POP) verses Object Oriented Programming (OOP)
1.2 Basic concepts of Object Oriented Programming, Object Oriented Languages, Applications of OOP.
1.3 C verses C++, Structure of C++ program, Simple C++ Program
1.4 Tokens, keywords, variables. Constants, basic data types, User defined data types, type casting,
operators, expressions.
1.5 Control structures: Decision making statements and Loop

1.6 Scope resolution operator, memory management operators. Arrays, Strings and Structures in C++

1.7 Arrays, Strings and Structures in C++


.

Q Difference between OOP and POP. (Any four points are expected, each point –1 Mark)
Ans:
Procedure oriented programming Object oriented programming

1. Focus is on procedure. 1. Focus is on data.

2. Large programs are divided into multiple 2. Programs are divided into multiple objects.
functions.

3. Data move openly around the system 3. Data is hidden and cannot be accessed by
from function to function. external functions.

4. Functions transform data from one form 4. Objects communicate with each other through
to another by calling each other. function.

5. Employs top-down approach in program 5. Employs bottom-up approach in program


design. design

6. Procedure oriented approach is used in C 6. Object oriented approach is used in C++


language. language.

SEE U IN C++ ALI KARIM SIR CODINGBOSS YOUTUBE CHANNEL 1


OOP Chapter 1 Ali Karim Sir 9220990566

Q Write any two characteristics of object oriented programming.


Q. What are the features of object oriented programming? (S17) 4m
Ans:
1. Emphasis is on data rather than procedure.
2. Programs are divided into objects.
3. Data structures are designed such that they characterize the objects.
4. Functions that operate on data of an object are tied together in the data structure.
5. Data is hidden and cannot be accessed by external functions.
6. Objects communicate with each other through functions.
7. New data and functions can be easily added whenever necessary.
8. Follows bottom-up approach in program design

Q Describe any four basic concepts of OOP. (S16) (4m)


Ans:
Basic Concepts of Object Oriented Programming:
Classes:
A class is the collection of related data and function under a single name. A class is collection
of object of similar type. The entire set of data and code of an object can be made a user-defined data
type with the help of class.
Eg.
class Employee
{

..
};

Objects:
Objects are the basic run time entities in an object-oriented system. They may represent a
person, a place, a bank account, a table of data or any item that the program has to handle. When a
program is executed, the objects interact by sending messages to one another.
Eg.
void main()
{
Employee e;


}

Data Abstraction and Encapsulation:


Abstraction refers to the act of representing essential features without including the
background details or explanation.
Encapsulation is hiding complexity and wrapping up of data & function into a single unit
called class.

Inheritance:
Inheritance is the process by which objects of one class acquired the properties of objects of
another classes. In OOP, the concept of inheritance provides the idea of reusability.

SEE U IN C++ ALI KARIM SIR CODINGBOSS YOUTUBE CHANNEL 2


OOP Chapter 1 Ali Karim Sir 9220990566

Polymorphism:
Polymorphism means the ability to take more than on form. An operation may exhibit different
behavior is different instances.

Dynamic Binding:
Binding refers to the linking of a procedure call to the code to be executed in response to the
call.

Message Passing:
An object-oriented program consists of a set of objects that communicate with each other.
Objects communicate with one another by sending and receiving information. Example:
Employee.Salary (name);

.Q List any four object oriented languages. (Each language – ½ Mark)


Ans: object oriented languages:
1. Simula 2. Smalltalk 3. C++ 4. Ada 5. Java 6. C#

Q State any four applications of OOP. (Any four applications; each carry - 1 Mark)
Ans:
Applications Object oriented programming are as follows
 Real time systems
 Simulation and modeling
 Object oriented databases
 Hypertext, hypermedia and expertext
 AI and expert systems
 Neural networks and parallel programming
 Decision support and office automation systems
 CIM/CAM/CAD systems.

Q With suitable diagram describe structure of C++ program. (Diagram - 1 Mark; Description -
3 Marks)
Ans:
General C++ program has following structure.
INCLUDE HEADER FILES
DECLARE CLASS
DEFINE MEMBER FUNCTIONS
DEFINE MAIN FUNCTION

Description:-
1. Include header files
In this section a programmer include all header files which are require to execute given program. The
most important file is iostream.h header file. This file defines most of the C++ statements like cout
and cin. Without this file one cannot load C++ program.

2. Declare Class
In this section a programmer declares all classes which are necessary for given program. The
programmer uses general syntax of creating class.

SEE U IN C++ ALI KARIM SIR CODINGBOSS YOUTUBE CHANNEL 3


OOP Chapter 1 Ali Karim Sir 9220990566

3. Define Member Functions


This section allows programmer to design member functions of a class. The programmer can have
inside declaration of a function or outside declaration of a function.

4. Define Main Functions


This section the programmer creates objects and calls various functions writer within various class.

Q Define class with its syntax. (Class definition – 1 Mark, Syntax – 1 Mark)
Ans:

Definition: A class is a way of binding data and its associated functions together.

Syntax:

class class_name
{
private:
Variable declarations;
Function declarations;
public:
Variable declarations;
Function declarations;
};

Q Describe syntax of ‘cin’ and ‘cout’ with example. (cin syntax - 1 Mark, example - 1 Mark; cout
syntax - 1 Mark, example - 1 Mark)
Ans:
• cin = cin statement is use to accept input from user.
Syntax:-
cin>>variable_name;
Example:
cin>>i;

• cout = cout statement is use to display output processed by computer or to display simple
messages to user.
Syntax:-
cout<<variable_name;
cout<<”Message”;
Example:
cout<<”Hello world”;
cout<<x;

SEE U IN C++ ALI KARIM SIR CODINGBOSS YOUTUBE CHANNEL 4


OOP Chapter 1 Ali Karim Sir 9220990566

Q. Explain insertion and extraction operators. 4m


Ans.
Insertion operators: The operator << is called the put to operator. It inserts (or sends) the contents of the
variable on its right to the object on its left.
Example: cout<< string;

Extraction operators: The operator >> is known as extraction or get from operator. It extracts (or takes)
the value from the keyboard and assigns it to the variable on its right.
Example: cin>> number1;

Q. Write simple C++ program to demonstrate class and objects.


Ans:
#include<iostream.h>
#include<conio.h>
class Example //Class name is Example
{
private:
int roll; //Member Variable
char name[10];
public:
void accept( ) //Member Function
{
cout<<"Enter Roll No.:";
cin>>roll;
cout<<"\n Enter Name of Students:";
cin>>name;
}
void display( ) //Member Function
{
cout<<"\n Roll Number is "<<roll;
cout<<"\n Name is "<<name;
}
}; //End of class
void main( )
{
clrscr();
Example d; // class Object ‘d’ is called
clrscr( );
d.accept( );
d.display( );
getch();
}
Output:

SEE U IN C++ ALI KARIM SIR CODINGBOSS YOUTUBE CHANNEL 5


OOP Chapter 1 Ali Karim Sir 9220990566

Q. Describe Token in cpp.


Ans:
A token is the smallest element of a program that is meaningful to the compiler. Tokens can be
classified as follows:
1. Keywords 4. Strings
2. Identifiers 5. Special Symbols
3. Constants 6. Operators

Q. List C++ Keyword.


Ans:
In C++ there are 31 additional keywords other than C Keywords they are:
asm bool catch class const_cast delete dynamic_cast explicit export
false friend inline mutable namespace new operator private protected
public reinterpret_cast static_cast template this throw true try
typeid typename using virtual wchar_t

SEE U IN C++ ALI KARIM SIR CODINGBOSS YOUTUBE CHANNEL 6


OOP Chapter 1 Ali Karim Sir 9220990566

Q. List Types of Loop.


Ans:

Q What is the significance of scope resolution operator? (Use of scope resolution operator – 2
Marks)
Ans: Scope resolution operator allows access to the global version of a variable, and also allows
facility to define member functions outside of a class.
It is represented with double colon (:: ).

Q. Write a program to declare a class ‘employee’ having data members name and age.
Ans:
Accept and display the data for three objects.
#include<iostream.h>
#include<conio.h>
class Emp
{
private:
int sal;
char name[20];
static float tot;

SEE U IN C++ ALI KARIM SIR CODINGBOSS YOUTUBE CHANNEL 7


OOP Chapter 1 Ali Karim Sir 9220990566

public:
void getdata(); //Member function declare inside class
void putdata(); //using scope resolution operator
void show();
};
void Emp::getdata() //Member function define outside class
{ //using scope resolution operator
cout<<"Enter name:";
cin>>name;
cout<<"salary";
cin>>sal;
}

void Emp::putdata() //Member function define outside class


{ //using scope resolution operator
cout<<name;
cout<<sal;
tot=tot+sal;
}

void Emp::show() //Member function define outside clas


{ //using scope resolution operator
cout<<"total:"<<tot;
}
void main()
{
clrscr();
Emp e1,e2,e3; //3 Objects are created of class Emp
e1.getdata();
e2.getdata();
e3.getdata();

e1.putdata();
e2.putdata();
e3.putdata();

e1.show();
getch();
}

Q List two memory management operators available in C++ and state its use in one line. (1-
Mark for new; 1- Mark for delete)
Ans:

SEE U IN C++ ALI KARIM SIR CODINGBOSS YOUTUBE CHANNEL 8


OOP Chapter 1 Ali Karim Sir 9220990566

There are two types of memory management operators in C++:


new:- use for allocating memory;
delete:- use for deleting/ free allocated memory space.

Q How memory is allocated to the objects of class, explain with diagram (Description - 3 Marks;
diagram - 1 Mark)
Ans:

 Memory space for objects is allocated when they are declared. The member functions are created
and placed in the memory space only once when they are defined as a part of class specification.
 Since all the objects belonging to that class use the same member functions, no separate space is
allocated for member functions when the objects are created.
 Only space for member variables is allocated separately for each object.
 Separate memory locations for the objects are essential, because the member variables will hold
different data values for different objects.
Q. Give syntax and example of defining structure and declaring structures variables.
Ans:
Definition:-
Structure is a collection of different data types written under a common name. It is a user defined data
type

Q Define a structure with it’s syntax. (Definition – 1 Mark, Syntax – 1 Mark)


Ans:
Structure is a collection of different data types written under a common name. It is a user defined data
type.
Syntax:-
struct structure_name
{ Data_type variable 1;
Data_type variable 2;
Data_type variable n;
}structure_variable1,…,structure_variable n;

SEE U IN C++ ALI KARIM SIR CODINGBOSS YOUTUBE CHANNEL 9


OOP Chapter 1 Ali Karim Sir 9220990566

Example:
struct book OR
{ struct book
char book_name[10]; {
char author_name[10]; char book_name[10];
float price; char author_name[10];
}b; float price;
};
void main()
{ void main()
b.book_name; {
b.author_name; struct book b;
} b.book_name;
b.author_name;
}

Q Differentiate between structure and class. (Any four points of comparison - 1 Mark each)
Ans:

Sr. Structure Class


No
1 Structure is collection of variable of Class is collection of variables of different
different data types data types & functions
2 “struct” keyword is used while declaring “class” keyword is used while declaring a
structure class
3 Structure variables can be created Class variables can be created which are
called as objects of class
4 Access specifiers are not available Access specifiers like private, public,
protected are available
5 Data hiding is not achieved Data hiding is achieved with private
keyword
6 Syntax: Syntax:
struct struct_name { class class_name {
variable 1; private:
variable 2; member variable declaration;
…. member function declaration;
variable n;
}struct_var; public:
member variable declaration;
member function declaration;
};

SEE U IN C++ ALI KARIM SIR CODINGBOSS YOUTUBE CHANNEL 10


OOP Chapter 1 Ali Karim Sir 9220990566

Question Bank of OOP


Chapter 1
Q1. Difference between OOP and POP. 4m
Q2. What are the features of procedure oriented programming. 4m
Q3. Describe any four basic concepts of OOP. 4m
Q4. List any four object oriented languages. 4m
Q5. State any four applications of OOP. 2m
Q6. With suitable diagram describe structure of C++ program. 4m
Q7. What is the significance of scope resolution operator? 2m
Q8. List two memory management operators available in C++ and state its
use in one line. 2m
Q9. How memory is allocated to the objects of class, explain with diagram.
4m
Q10. Define a structure with its syntax. 4m

SEE U IN C++ ALI KARIM SIR CODINGBOSS YOUTUBE CHANNEL 11

You might also like