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

Chapter 7-Classes and Objects (38-50)

1. The document discusses classes and objects in C++, providing examples of class definitions, member functions defined inside and outside classes, and advantages of object-oriented programming. 2. Key points covered include the syntax for declaring objects, defining classes with public, private and protected members, and accessing class members using dot operators. 3. Advantages of OOP highlighted are modularity, encapsulation, data abstraction, and ease of code maintenance through inheritance and polymorphism.

Uploaded by

Khushi Y.S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
282 views

Chapter 7-Classes and Objects (38-50)

1. The document discusses classes and objects in C++, providing examples of class definitions, member functions defined inside and outside classes, and advantages of object-oriented programming. 2. Key points covered include the syntax for declaring objects, defining classes with public, private and protected members, and accessing class members using dot operators. 3. Advantages of OOP highlighted are modularity, encapsulation, data abstraction, and ease of code maintenance through inheritance and polymorphism.

Uploaded by

Khushi Y.S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Sri Chaitanya Educational Institutions, India

CLASSES AND OBJECTS


Classes and
(1 marks) (2 marks) (3 marks) (5 marks) Total Marks
objects

01 - - 01 06

One mark questions:


1. Write the syntax to declare objects of a class.
A. Class_name object_name;
2. What is an object?
A. Real world entity with attribute and function.
3. What is a class?
A. A class is a structured data type in C++ which is a collection of variables and functions.
(or)
A class is a collection of similar type of objects.
4. Which type of data members are accessible outside the class?
A. Public and protected.
5. What is the significance of scope resolution operator (::) ?
A. The scope resolution operator (::) is used when a member function is defined outside the
class.
6. Define a Class?
A. A class is a user defined datatype that contains data members and objects.
7. Mention the operator used to access members of a class
A. Class member can be accessed by using dot (.) operator.
TWO marks questions:
8. Give the general syntax for defining classes and objects.
A. The syntax of class definition :
class name_of_the class
{
private:
data members(variables);
functions (member functions);
protected:

Page 38
Sri Chaitanya Educational Institutions, India
data members(variables);
functions (member functions);
public:
data members(variables);
functions (member functions);
};
Object Creation:
Class_name object_name;
9. Briefly discuss the classes of objects ?
A. The class allow to group functions and data variables. The class is a way of grouping
objects having similar characteristics.
The objects can be made user defined data types with the help of a class.
10. What is the fundamental idea of OOP? Mention any two Object Oriented Programming
languages.
A. The fundamental idea behind object oriented programming is to combine both data and
functions that operate on that data into a single unit.
The object oriented programming languages are C++, java, C# etc.
11. Mention any two advantages of object oriented programming over earlier programming
methods.
A. a) OOP can communicate through message passing which makes interface description
with outside system very simple.
b) The concept of data abstraction separates object specification and object
implementation.
c) Creation and implementation of OOP code is easy.
FIVE marks questions:
12. Explain the member function inside the class definitions and outside the class
definitions.
A. Outside the class definition
Member functions that are declared inside a class have to be defined separately outside
the class.
The general form of a member function definition is:

Page 39
Sri Chaitanya Educational Institutions, India
Return type class_name :: function_name(argument declaration)
{
Function body;
}
The class_name :: function_name tells the compiler that the scope of the function
Is restricted to the class_name specified in the header line. The symbol :: is called the
scope resolution operator.
For example :
class simple
{
Float p,t,r,si;
public:
void getdata(); // declaration outside the class
void calculate();//outside the class
void display();
};
void simple::getdata()
{
Cout<<”enter p,t,r values”;
Cin>>p>>t>>r;
}
Void simple::calculate()
{
Si=(p*t*r)/100;
}
Void simple::display()
{
Cout<<”prinicipal amount is=”<<<P;
Cout<<”timeperoid =”<<t;
Cout<<”rate of interst =”<<r;
Cout<<”simple interst is =”<<si;

Page 40
Sri Chaitanya Educational Institutions, India
}
Void main()
{
Simple obj;
Clrscr();
Obj.getdata();
Obj.calculate();
Obj.display();
Getch():
}
Output:
Enter p,t,r values
1000 2 3
Principal amount=1000
Time period =2
Rate of interst=3
Simpleinterst is=60
Explanation
Since these functions do not return any value, their return_type is void. The member
functions have some special characteristics that don’t return any value, their return_type
is void. The member functions have some special characteristics that are often used in
the program development. These characterstics are often used in the program
development.
These characteristics are :
Several different classes can use the same function name. The ‘membership label’ will
resolve their scope.
Member functions can access the private data of the class. A non-member functions
cannot do so. However, an exception to this rule is a friend function.
A member function can call another member function directly, without using the dot
operator.

Page 41
Sri Chaitanya Educational Institutions, India
The type and number of arguments in the member function must be same as the type and
data type declared in the class definition.
Member function can access private data of a class, but a non-member function can not.
Inside the class definition:
Another method of defining a function definition inside the class. For example, we
could define the item class as follows:
class simple
{
Float p,t,r,si;
public:
void getdata() //declaration inside the class

{
Cout<<”enter p,t,r values”;
Cin>>p>>t>>r;

}// declaration inside the class


void calculate();// declaration outside the class
void display();
};
Void simple::calculate()
{
Si=(p*t*r)/100;
}
Void simple::display()
{
Cout<<”prinicipal amount is=”<<<P;
Cout<<”timeperoid =”<<t;
Cout<<”rate of interst =”<<r;
Cout<<”simple interst is =”<<si;
}

Page 42
Sri Chaitanya Educational Institutions, India
Void main()
{
Simple obj;
Clrscr();
Obj.getdata();
Obj.calculate();
Obj.display();
Getch():
}
13. Mention the advantages of object oriented programming
A. 1) The programs are modularized based on the principle of classes and objects.
2) Data is encapsulated along with functions. Therefore external non-member function
cannot access or modify data, thus providing data security
3) The concept of data abstraction separates object specification and object
implementation.
4) OOP provides a clear modular structure for programs. Large problems can be
reduced to smaller and more manageable problems.
5) In OOP, data can be made private to a class that only member functions of the class
can access the data. This principle of data hiding helps the programmer to build a secure
program.
6) It is easy to maintain and modify existing codes as new objects can be created with
small differences to existing ones.
14. Define object oriented programming. Write the limitations of it.
A. Object oriented programming (OOP) is a programming language model organized
around “objects” rather than “actions” and data rather than logic of the program.
Limitations:
Size: Object oriented programs are much larger than other programs.
Effort : Object oriented programs require a lot of work to create.
Speed: Object oriented programs are slower than other programs, because of its size.
Not suitable to all types of problems.

Page 43
Sri Chaitanya Educational Institutions, India
15. What is class definition? Write its general syntax and example.
A. A class definition is a process of naming a class and data variables and interface
operations of the class. A class is a structured data type in C++ which is a collection of
variables and functions.
The syntax of a class definition:
class name_of_the_class
{
private:
data members(variables);
functions (member functions);
protected:
data members(variables);
functions (member functions);
public:
data members(variables);
functions (member functions);
};
Example:
class student
{
private:
int regno, fees;
char name[50];
public:
void getdata();
void printdata();
};
16. Explain defining objects of a class with syntax and a programming example.
A. A class definition is a process of naming a class and data variables and interface
operations of the class. A class is a structured data type in C++ which is a collection of
variables and functions.

Page 44
Sri Chaitanya Educational Institutions, India
Object is an instance of a class.
The syntax of a class definition:
class name_of_the_class
{
private:
data members(variables);
functions (member functions);
protected:
data members(variables);
functions (member functions);
public:
data members(variables);
functions (member functions);
};
Example:
class student
{
private:
int regno, fees;
char name[50];
public:
void getdata();
void printdata();
};

17. Explain any five characteristics of opps.


A. 1. Abstraction : Abstraction refers to the representation of the essential features of an
object as a program object. An abtract class defines an interface but does not provide
implementation details.
2. Encapsulation : Encapsulation is a way of combining data and associated functions
into a single unit.

Page 45
Sri Chaitanya Educational Institutions, India
3. Inheritance : It is the capability of a class to inherit the properties of another class.
The class that inherits the properties from another class it is known a derived class. The
class that provides its properties to a sub class is known an base class.
4. Message passing : The processing of data in object oriented programming is carried
out by sending messages to objects.
5. polymorphism : Polymorphism is ability of a function to have same name and
multiple forms. The function is called automatically by the computer depending on
number and type of arguments.

18. A class ‘clock’ has following members. (a) Hour (b) Minute Create the calls with
member functions for
a) Initializing the data members
b) Display the time
c) Convert Hours and Minutes to minutes.
A. Create the class with member functions for
a) Initializing the data members
b) Display the time
c) Convert Hours and Minutes to minutes.
class clock
{
private:
int hour, minute;
public:
void getdata( )
{
cout<< “ Enter the hours:”;
cin>>hour;
cout<< “ Enter the minutes:”;
cin>>minute;
}
void caldata( );

Page 46
Sri Chaitanya Educational Institutions, India
void display( );
};
void clock::caldata ( )
{
minute = minute + hours * 60;
}
void clock::display( )
{
cout<< “Total minutes =”<<minute;
}
};
Void main()
{
Clock obj;
Obj.getdata();
Obj.caldata();
Obj.display();
Getch();
}
Output
Enter the hours 1
Enter the minutes :30
Total minutes =90
19. Explain class definition and class declaration with syntax and example.
A. A class declaration specifies the representation of objects of class and set of operation
that can be applied to such object.
General syntax for defining a class
Body of class
Class class_name
{
Private:

Page 47
Sri Chaitanya Educational Institutions, India
Member data
Member function
Protected:
Member data
Member function
Public:
Member data
Member function
};
Where
The keyword class is used to declare a class. The class_name is any valid identifier used
to identify the name of the class.
The class body enclosed in a pair of flower brackets. A class body contain declaration of
its members like data and function and access specifier.
Access specifier
The access specifier define the scope of the member data and member function. We use
access protection for hiding data and function
Example:
class point
{
int x,y;
public:
void setpoint(int, int);
void display();
};
The above example contains the class name as point with 2 private data members x and
y of type integer. The key word public specifies that from that point onwards whatever
declared is public so we have 2 public member function in the class. The end of flower
bracket indicate end of class body.
Creating objects: Once a class has been declared, we can create variables of that type by
using the class name (like any other built in type variable).

Page 48
Sri Chaitanya Educational Institutions, India
For example,
Point x; // memory for x is created
Creates a variable x of type point and the class variables are known as objects.
Therefore, x is called an object of type point. We may also declare more than one object
in one statement.
Example:
Point x, y, z;

20. Describe how objects can be used as function argumets with example.
A. A function can receive an object as a function argument.This is similar to any other data
type being sent as function argument.An object can be passed to a function in two ways.
i. Copy of entite object is passed to function.
ii. only address of the object is transferred to the function.
INPASS by value.a copy of the object is passed to the function .the function creates own
copy of the object and uses it.Therefore changes made to the object inside function do
not affect the original object.
INPassBy reference when an address of an object is passed to the function,the function
directly works on the original object used in the function call.This means changes made
to the object inside the function reflect in the original object,because the function is
making chages in the original object itself.
Passbyrefence is more efficient
#include<iostream.h?
#include<conio.h>
Class srup
{
Private:
int n1,n2;
public:
rup();
n1(1),n2(1);
void get()

Page 49
Sri Chaitanya Educational Institutions, India
{
Cout<<”enter first number”
Cin>>n1;
Cout<<enter second number”;
Cin>>n2;
Void print()
{
Cout<<”product1=”<<n1<<endl;
Cout<<”product2=”<<n2<<endl;
}
Void multi(rup r1,rup r2)
{
n1=r1.n1*r1.n2;
n2=r2.n1*r2.n2;
};
Void main()
{
Rup r1,r2,r3;
Clrscr();
R1.get();
R2.get();
R3.multi(r1,r2);
R3.print();
getch();
}

Page 50

You might also like