Official Class 12 Computer Science Worksheets: Class And Objects
Access comprehensive chapter-wise worksheets for Class And Objects using the CBSE Class 12 Computer Science Class And Objects Worksheet Set 02. Designed to align with the 2026-27 academic syllabus for Class 12 Computer Science, these printable practice sets help students reinforce key concepts and improve their overall exam readiness.
Solved Practice Worksheets for Computer Science
Navigate directly to the solved Computer Science worksheets using the digital viewer below. Each practice set includes detailed step-by-step solutions, allowing students to instantly cross-check their work and identify areas requiring further revision.
1 Write the definition for a class called Rectangle that has floating point data members length and width. The class has the following member functions:
void setlength(float) to set the length data member
void setwidth(float) to set the width data member
float perimeter() to calculate and return the perimeter of the rectangle
float area() to calculate and return the area of the rectangle
void show() to display the length and width of the rectangle
int sameArea(Rectangle) that has one parameter of type Rectangle. sameArea returns 1 if the two Rectangles have the same area, and returns 0 if they don't.
1. Write the definitions for each of the above member functions.
2. Write main function to create two rectangle objects. Set the length and width of the first rectangle to 5 and 2.5. Set the length and width of the second rectangle to 5 and 18.9. Display each rectangle and its area and perimeter.
3. Check whether the two Rectangles have the same area and print a message indicating the result. Set the length and width of the first rectangle to 15 and 6.3. Display each Rectangle and its area and perimeter again. Again, check whether the two Rectangles have the same area and print a message indicating the result.
1. Define a class STUDENT with the following specifications
Private members:
Grno long integer
Name 20 characters
Marks in 5 subjects, each out of 100 an array of 5 float values
Total, percentage float
Calculate() Function to calculate total & percentage
Public functions
takedata() Function to read Grno, name and 5 marks of a student
displaydata() to invoke Calculate() function and to display all the details of the student
Write a Program to implement the above class for a student.
2. Define a class EMPLOYEE with the following specifications
Private data members
Empno integer
Name 20 characters
Basicpay float
HRA, DA, PF float
Netpay double
Public functions
Getdata() Function to read empno, name and basicpay of an Employee
Compute() Function to compute HRA (10% of basicpay), DA (50% of basicpay), PF (8% of basicpay) and netpay as basicpay + HRA + DA – PF and return the calculated Netpay.
Printdata() to invoke Compute() function and to display all the details of an employee
Write a Program to read details (empno, name and basicpay) for n employees and compute netpay for them and display all the details.
CLASSES AND OBJECTS
Q1. What is class? What is its need? Classes
A class is a way to bind the data describing an entity and its associated functions together. In C++ , class makes a data type that is used to create objects of this type. Need for classes
Classes are needed to represent real-world entities that not only have data type properties (Characteristics) but also have associated operations (their behaviour)
Q2 . Differentiate between local and global class with help of example.
Answer : 1. Global Class
A class is said to be global it its declaration occurs outside the bodies of all functions in a program.
2. Local class
A class is said to be local if its occurs inside the function body
# include<iostream.h>
class x //x is a global class
{
members variable functions of class;
};
x ob1; //global object
int main ( )
{
class num // num is local class
{
public:
int a=10;
void num( ) // constructor (same name of class)
{
cout<<num;
};
x ob2; //local object of class x
num n1; // n1is a class object
n1.num( );
} // closing of main ()
Q3. What do you mean by nested class ?Explain.
Answer : Nested class
Declaration of one class in to another class is called is called nested of a class.
#include<iostream.h>
#include<conio.h> // for clrscr()
class score
{
int a; //private member variable
class batesman // nested class
{
int b;
public:
int c;
void total( )
{
cin>>b>>c;
int sum = b+c;
}
batesman( )
{
c=10;
}
}; // end of class batesman
batesman obj2; // object of class batesman
void second( void)
{
cout<<score :: second()<<endl;
cout<<”A =” <<a
}
score( )
{
a= 25;
}
}; //end of class score;
void main()
{
score ab;
batesman bc;
score :: batesman cd;
ab.second();
bc.total();
cd.total();
}
Q4. What are the advantages and disadvantages of inline functions?
Answer : The main advantages of inline functions is that they save on the overheads of a function call as the function is not invoked, rather its code is replaced in the program.
The major disadvantage of inline functions is that with more function calls, more memory is wasted as for every function call, the same function code is inserted in the program. Repeated occurrences of same function code waste memory space.
Q5. What do you understand by member function?
Answer : Member function have full access privilege to both the public and private members of the class. These are defined within the class scope that is they are not visible outside the scope of class.
But not member functions are visible outside the class (ordinary functions)
Q6. What are static class members?
Answer : A class can have static data members as well as static member functions The static data members are the class variables that are common for all the objects of the class. Only one copy of static data members is maintained which is shared by all the objects of the class. They are visible only with in the class
Q7. Identify the errors in the following code fragment:
class ab
{
int x;
static int ctr;
Public:
Void init(void) {
x=ctr=0; }
static void print(void) {
cout<<ctr<<x; } };
Answer : In the above code fragment, a static member function is trying to access a non static member that is ‘x’ which is the error
The correct code for static member function is
static void print(void)
{
cout<<ctr; }
Q8. What is the significance of access specifiers in a class?
Answer : A class provides three access specifiers namely private, public and protected
(a) A data member declared as private or protected remains hidden from outside world and it can only be accessed by the member functions of the class.
(b) A data member declared as public is made available to the outside world. That is, it can be accessed by any function, any expression in the program but only by using an object of the same class type.
These access labels enforce data hiding and abstraction also.
Q9 . Write the output of the following code
#include<iostream.h>
class counter {
private:
unsigned int count;
public:
counter() // constructor
{
count=0; }
void inc_count( ) // increment in count variable
{
count ++; }
int get_count( ) {
return count; }
};
void main()
{
counter c1,c2;
cout<< “ \n value for c1 is:” <<c1.get_count( );
cout<< “ \n value for c2 is:” <<c2.get_count( );
c1.inc_count();
c2.inc_count();
c2.inc_count();
cout<< “\t c1=” <<c1.get_count();
cout<< “\t c2=” <<c2.get_count(); }
Answer : The output will be
c1=0 c2=0 c1=1 c2=2
Chapter – 4 CONSTRUCTOR AND DESTRUCTORS
Q1. What is Constructor ?
Answer : Constructors:
A constructor is a special member function of a class that is automatically called, when an object is created of that class. A member function with the same name as its class is called constructor.
Q2. What is destructor ? What is its need ?
Answer : Destructor:
A destructor is also a member function whose name is the same as the class name but is preceded by tilde (‘~’)
A destructor takes no arguments and no return types can be specified for it. It is called automatically by the compiler when an object is destroyed. A destructor cleans up the storage ( memory area of the object) that is no longer accessible. Need for Destructors
Allocated resources (like constructor) must be de-allocated before the object is destroyed. It works as a de-allocating and releasing memory area and it perform our works as a clean up tasks. Therefore, a destructor is equally useful as a constructor is.
class stud
{
stud() //constructor
{
cout << “ welcome”
Question 1. Define a class STUDENT with the following specifications
Private members:
Grno long integer
Name 20 characters
Marks in 5 subjects, each out of 100 an array of 5 float values
Total, percentage float
Calculate() Function to calculate total & percentage
Public functions
takedata() Function to read Grno, name and 5 marks of a student
displaydata() to invoke Calculate() function and to display all the details of the student
Write a Program to implement the above class for a student.
Answer:
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
class STUDENT {
private:
long int Grno;
char Name[20];
float Marks[5];
float Total;
float percentage;
void Calculate() {
Total = 0.0f;
for (int i = 0; i < 5; i++) {
Total += Marks[i];
}
percentage = Total / 5.0f;
}
public:
void takedata() {
cout << "Enter Admission Number (Grno): ";
cin >> Grno;
cin.ignore(); // Clears newline character from the stream buffer
cout << "Enter Student Name: ";
cin.getline(Name, 20);
cout << "Enter Marks for 5 subjects (each out of 100):\n";
for (int i = 0; i < 5; i++) {
cout << "Subject " << (i + 1) << ": ";
cin >> Marks[i];
}
}
void displaydata() {
Calculate(); // Calling private helper method
cout << "\n----------------------------------------\n";
cout << "STUDENT REPORT DETAILS\n";
cout << "----------------------------------------\n";
cout << "GR No. : " << Grno << "\n";
cout << "Student Name : " << Name << "\n";
cout << "Marks Secured: ";
for (int i = 0; i < 5; i++) {
cout << Marks[i] << " ";
}
cout << "\nTotal Score : " << Total << " / 500\n";
cout << "Percentage : " << fixed << setprecision(2) << percentage << "%\n";
cout << "----------------------------------------\n";
}
};
int main() {
STUDENT studentObj;
studentObj.takedata();
studentObj.displaydata();
return 0;
}
In simple words: This program defines a student class that stores details, aggregates subjects privately using a helper function, and shows a neatly formatted progress card.
Exam Tip: Be sure to call your internal calculation function `Calculate()` from within the public output method, as private class functions are completely inaccessible directly from the main function.
Question 2. Define a class EMPLOYEE with the following specifications
Private data members
Empno integer
Name 20 characters
Basicpay float
HRA, DA, PF float
Netpay double
Public functions
Getdata() Function to read empno, name and basicpay of an Employee
Compute() Function to compute HRA (10% of basicpay), DA (50% of basicpay), PF (8% of basicpay) and netpay as basicpay + HRA + DA - PF and return the calculated Netpay.
Printdata() to invoke Compute() function and to display all the details of an employee
Write a Program to read details (empno, name and basicpay) for n employees and compute netpay for them and display all the details.
Answer:
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
class EMPLOYEE {
private:
int Empno;
char Name[20];
float Basicpay;
float HRA;
float DA;
float PF;
double Netpay;
public:
void Getdata() {
cout << "Enter Employee Number: ";
cin >> Empno;
cin.ignore(); // Clears standard input buffer
cout << "Enter Employee Name: ";
cin.getline(Name, 20);
cout << "Enter Basic Pay: ";
cin >> Basicpay;
}
double Compute() {
HRA = 0.10f * Basicpay; // Calculating 10% House Rent Allowance
DA = 0.50f * Basicpay; // Calculating 50% Dearness Allowance
PF = 0.08f * Basicpay; // Calculating 8% Provident Fund deduction
Netpay = Basicpay + HRA + DA - PF;
return Netpay;
}
void Printdata() {
Compute(); // Computing allowances and net salary
cout << "\n----------------------------------------\n";
cout << "EMPLOYEE MONTHLY PAYSLIP\n";
cout << "----------------------------------------\n";
cout << "Employee No : " << Empno << "\n";
cout << "Employee Name : " << Name << "\n";
cout << "Basic Salary : Rs. " << fixed << setprecision(2) << Basicpay << "\n";
cout << "HRA (10%) : Rs. " << HRA << "\n";
cout << "DA (50%) : Rs. " << DA << "\n";
cout << "PF (8%) : Rs. " << PF << "\n";
cout << "Net Salary : Rs. " << Netpay << "\n";
cout << "----------------------------------------\n";
}
};
int main() {
int n;
cout << "Enter the number of employees to process: ";
cin >> n;
// Creating dynamic array to store employee objects
EMPLOYEE* empArray = new EMPLOYEE[n];
for (int i = 0; i < n; i++) {
cout << "\nRecording details for Employee " << (i + 1) << ":\n";
empArray[i].Getdata();
}
cout << "\n========================================\n";
cout << " PAYROLL LOG SUMMARY \n";
cout << "========================================\n";
for (int i = 0; i < n; i++) {
empArray[i].Printdata();
}
delete[] empArray; // Deallocating dynamic array memory
return 0;
}
In simple words: This code creates a payroll program for multiple employees. It processes basic salaries, computes custom benefits and deductions, and outputs distinct payslips.
Exam Tip: Ensure your `Compute()` function has a non-void return statement since the problem specifications explicitly require it to return the calculated `Netpay` value.
Free study material for Computer Science
Free CBSE Practice Worksheets: Class 12 Computer Science Class And Objects
Mastering Class And Objects with Printable Worksheets
Review targeted practice exercises for Class 12 Computer Science Class And Objects. Curated to match official CBSE guidelines, these printable problem sets support daily revision and improve overall test readiness.
Verified Solutions and NCERT Alignment
Built using official NCERT guidelines for Class 12 Computer Science, these practice sheets provide reliable academic support. Cross-reference your completed work with our detailed solutions to learn standard answer-writing formats for CBSE exams.
Additional Study Resources for Class 12 Computer Science
Follow up your worksheet practice by attempting the interactive online MCQ tests for Class And Objects to evaluate your execution speed. All printable assignments and revision sheets on our platform are updated for the 2026 session and available free of charge.
FAQs
You can download the latest chapter-wise printable worksheets for Class 12 Computer Science Class And Objects for free from StudiesToday.com. These have been made as per the latest CBSE curriculum for this academic year.
Yes, Class 12 Computer Science worksheets for Class And Objects focus on activity-based learning and also competency-style questions. This helps students to apply theoretical knowledge to practical scenarios.
Yes, we have provided solved worksheets for Class 12 Computer Science Class And Objects to help students verify their answers instantly.
Yes, our Class 12 Computer Science test sheets are mobile-friendly PDFs and can be printed by teachers for classroom.
For Class And Objects, regular practice with our worksheets will improve question-handling speed and help students understand all technical terms and diagrams.