Notes Paper6
Notes Paper6
differences:
C is a procedural programming language, which means it focuses on procedures or functions to
operate on data.
C++ supports both procedural and object-oriented programming paradigms. It introduces
classes and objects, l
encapsulation, inheritance, and polymorphism.
Abstraction:C provides basic data types and structures for organizing data but lacks built-in
support for abstraction.
C++ supports abstraction through classes and objects, allowing for better organization and
modularization of code.
Encapsulation:C does not support encapsulation inherently. Data and functions are often
declared globally or within
structures, but there are no access control modifiers.C++ supports encapsulation through
access specifiers like public,
private, and protected, allowing the hiding of implementation details and better control over
access to class members.
Inheritance:C does not support inheritance.C++ supports inheritance, allowing a class to inherit
properties and behaviors
from another class. This facilitates code reuse and promotes the creation of hierarchies of
related classes.
Polymorphism:C does not support polymorphism.C++ supports polymorphism through function
overloading and virtual functions.
This enables functions to behave differently based on the object they are called with, enhancing
code flexibility and readability.
Standard Libraries:Both C and C++ have their own standard libraries, but C++ includes
additional libraries to support object-oriented
features and provides more extensive functionalities.
Compatibility:C code can generally be compiled by a C++ compiler, but the reverse is not
always true due to C++'s additional features.
C++ introduces new keywords and constructs that are not present in C, so C code might need
adjustments to be compiled as C++.
Memory Management:Both languages support manual memory management, but C++
introduces the concept of constructors and destructors, making
memory management more automated through the use of objects and classes.In summary,
while C and C++ share some similarities, C++ expands upon
C by adding support for object-oriented programming, which brings features like encapsulation,
inheritance, and polymorphism, making it more
suitable for complex and modular software development.
public:
Person(string n, int a) {
name = n;
age = a;
}
void setName(string n) {
name = n;
}
void setAge(int a) {
age = a;
}
string getName() {
return name;
}
int getAge() {
return age;
}
void displayInfo() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
int main() {
Person person1("Alice", 25);
Person person2("Bob", 30);
cout << "Information about Person 1:" << endl;
person1.displayInfo();
cout << "\nInformation about Person 2:" << endl;
person2.displayInfo();
person1.setName("Carol");
person1.setAge(35);
cout << "\nUpdated information about Person 1:" << endl;
person1.displayInfo();
return 0;
}
In this program:
We define a class Person with private data members name and age, and public member
functions (methods) to set and get these
attributes, as well as a function
to display information about the person.In the main() function, we create two Person objects
(person1 and person2) with
different initial values for name and age.We then display information about these objects using
the displayInfo() method.
We also demonstrate how to update the information of a Person object using the setName() and
setAge() methods,
followed by displaying the updated information.
6:- Write a program in C++ using a member function passing object of class to display smallest
between two number.
Here's a C++ program that demonstrates passing objects of a class to a member function to
display the smallest between two numbers:
#include <iostream>
using namespace std;
class Number {
private:
int num1, num2;
public:
Number(int n1, int n2) {
num1 = n1;
num2 = n2;
}
void displaySmallest() {
if (num1 < num2) {
cout << "The smallest number is: " << num1 << endl;
} else {
cout << "The smallest number is: " << num2 << endl;
}
}
};
int main() {
Number numbers(10, 5);
numbers.displaySmallest();
return 0;
}
In this program:
We define a class called Number, which has two private member variables num1 and num2.
The class has a constructor that initializes these member variables.
The class also has a member function displaySmallest() that compares the two numbers and
displays the smallest one.
In the main() function, we create an object numbers of the Number class with initial values 10
and 5.
We then call the displaySmallest() member function on the numbers object to display the
smallest number between the two.
7:- Write a program using class and object in java to check a number is Palindrome or not.
Sure, here's a Java program using a class and object to check if a number is a palindrome or
not:
import java.util.Scanner;
class PalindromeNumber {
public boolean isPalindrome(int number) {
int originalNumber = number;
int reverse = 0;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number /= 10;
}
return originalNumber == reverse;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
PalindromeNumber palindromeNumber = new PalindromeNumber();
boolean isPalindrome = palindromeNumber.isPalindrome(number);
if (isPalindrome) {
System.out.println(number + " is a palindrome number.");
} else {
System.out.println(number + " is not a palindrome number.");
}
scanner.close();
}
}
In this program:
We define a class called PalindromeNumber with a member function isPalindrome() that checks
whether a given
number is a palindrome or not.Inside the isPalindrome() method, we reverse the given number
and compare it with
the original number to determine if it's a palindrome.In the main() method, we prompt the user to
enter a number.
We create an object palindromeNumber of the PalindromeNumber class.We call the
isPalindrome() method on the object
to check if the entered number is a palindrome or not, and then display the result accordingly.
8:- Write _any five features of Java.
Default Constructor:
A default constructor is automatically provided by Java if no constructor is explicitly defined in a
class.
It initializes the object with default values (0, null, false) for primitive data types and null for
reference data types.
Its signature is public ClassName() { }.
Parameterized Constructor:
A parameterized constructor is a constructor with parameters used to initialize the object with
specific values.
It allows for custom initialization of objects based on the values passed as arguments.
Its signature includes parameters and looks like public ClassName(parameter1Type
parameter1, parameter2Type parameter2, ...) { }.
Copy Constructor:
A copy constructor is used to create a new object as a copy of an existing object.
It initializes the new object with the same state as the existing object.
Its signature includes a single parameter of the same type as the class, and it typically performs
a deep copy of the object.
For example:
public ClassName(ClassName originalObject) {
}
Constructor Overloading:
Constructor overloading is the practice of having multiple constructors in a class with different
parameter lists.
It allows objects to be initialized in different ways, providing flexibility and convenience to the
users of the class.
The constructors must have unique parameter lists to avoid ambiguity.
Example of constructor overloading:
java
Copy code
public ClassName() { }
public ClassName(int x) { }
public ClassName(int x, int y) { }
Constructors play a crucial role in Java programming as they ensure that objects are properly
initialized before they are used.
They help maintain the integrity of objects by setting their initial state, and their flexibility allows
for various
initialization scenarios to be accommodated.
10:- Write a program using constructor to take two number and display arithmetical operation.
Here's a Java program that uses a constructor to take two numbers and display arithmetic
operations:
import java.util.Scanner;
class ArithmeticOperations {
private int number1;
private int number2;
public ArithmeticOperations(int num1, int num2) {
number1 = num1;
number2 = num2;
}
public void displayOperations() {
System.out.println("Arithmetic Operations:");
System.out.println("Addition: " + (number1 + number2));
System.out.println("Subtraction: " + (number1 - number2));
System.out.println("Multiplication: " + (number1 * number2));
System.out.println("Division: " + (number1 / number2));
}
}
class Shape {
public:
// Pure virtual function to calculate area
virtual float calculateArea() = 0;
};
class Rectangle : public Shape {
private:
float length;
float width;
public:
Rectangle(float l, float w) : length(l), width(w) {}
float calculateArea() override {
return length * width;
}
};
class Circle : public Shape {
private:
float radius;
public:
Circle(float r) : radius(r) {}
float calculateArea() override {
return 3.14 * radius * radius;
}
};
int main() {
Rectangle rectangle(5, 4);
Circle circle(3);
cout << "Area of rectangle: " << rectangle.calculateArea() << endl;
cout << "Area of circle: " << circle.calculateArea() << endl;
return 0;
}
In this example:
We have an abstract class Shape with a pure virtual function calculateArea().
We have two derived classes, Rectangle and Circle, which inherit from the Shape class.
Each derived class provides its own implementation of the calculateArea() function.
We create objects of the derived classes (rectangle and circle) and call the calculateArea()
function on them.
The appropriate implementation of calculateArea() (from the derived class) is called based on
the object's type,
demonstrating polymorphic behavior.
12:- What is template class in C++? Write a program in C++ to print greatest between two value
integer, float and character.
A template class in C++ allows you to define a class with generic types, meaning you can use
the same class definition with
different data types without having to rewrite the code for each type. This enables you to create
flexible and reusable code
that can work with various data types.
Here's a program in C++ using a template class to print the greatest value between two values
of different types (integer, float, and character):
#include <iostream>
using namespace std;
template<typename T>
class Greatest {
private:
T value1;
T value2;
public:
Greatest(T v1, T v2) : value1(v1), value2(v2) {}
T findGreatest() {
return (value1 > value2) ? value1 : value2;
}
};
int main() {
Greatest<int> greatestInt(10, 20);
cout << "Greatest integer: " << greatestInt.findGreatest() << endl;
Greatest<float> greatestFloat(3.14, 2.718);
cout << "Greatest float: " << greatestFloat.findGreatest() << endl;
Greatest<char> greatestChar('A', 'B');
cout << "Greatest character: " << greatestChar.findGreatest() << endl;
return 0;
}
In this program:
We define a template class Greatest with a single type parameter T.
The class has two private member variables value1 and value2, which represent the two values
to compare.
The constructor initializes these member variables with the values passed as arguments.
The findGreatest() method compares the two values and returns the greatest value.
In the main() function, we create instances of the Greatest class for different data types (integer,
float, and character)
and call the findGreatest() method to print the greatest value for each type.
13:- Explain lnheritanee in C++ with some advantages and their types in Java.
Inheritance in C++ and Java is a mechanism by which a class (subclass or derived class) can
inherit properties and behaviors
(attributes and methods) from another class (superclass or base class). This enables code
reuse, promotes modularity,
and supports the concept of hierarchical classification.
Inheritance in C++:
In C++, inheritance is implemented using classes. Here's how inheritance works in C++:
Base Class (Superclass):
The base class is the class whose properties and behaviors are inherited by another class.
It defines the common attributes and methods shared by its subclasses.
Example:
class Shape {
protected:
int width;
int height;
public:
void setDimensions(int w, int h) {
width = w;
height = h;
}
};
Derived Class (Subclass):
The derived class is the class that inherits properties and behaviors from a base class.
It extends the functionality of the base class by adding new attributes and methods or by
overriding existing ones.
Example:
class Rectangle : public Shape {
public:
int calculateArea() {
return width * height;
}
};
Advantages of Inheritance in C++:
Code Reuse: Inheritance allows subclasses to inherit attributes and methods from their
superclass, reducing code duplication
and promoting modularity.Polymorphism: Inheritance facilitates polymorphism, allowing objects
of different subclasses to be treated as objects of the
same superclass type, promoting flexibility and extensibility.Modularity: Inheritance promotes
modularity by organizing classes into hierarchical
structures, making the codebase easier to understand and maintain.
Inheritance Types in Java:
In Java, inheritance operates similarly to C++. However, Java supports single inheritance (a
subclass can inherit from only one superclass)
and multiple inheritance through interfaces. Here are the types of inheritance in Java:
Single Inheritance:
In single inheritance, a subclass inherits from only one superclass.
Example:
class Subclass extends Superclass {
}
Multiple Inheritance through Interfaces:
Java supports multiple inheritance through interfaces, where a class can implement multiple
interfaces.
Interfaces define a contract that classes must adhere to by implementing their methods.
Example:
interface Interface1 {
void method1();
}
interface Interface2 {
void method2();
}
class MyClass implements Interface1, Interface2 {
}
Advantages of Inheritance in Java:
Code Reuse: Inheritance enables classes to inherit attributes and methods from their
superclass or interfaces, promoting code reuse and modularity.
Interface-Based Polymorphism: Java's support for interface-based inheritance allows for
polymorphic behavior, where objects of different classes
that implement the same interface can be treated uniformly.
Method Overriding: Inheritance allows subclasses to override methods inherited from their
superclass, enabling customization and specialization of behavior.
Overall, inheritance in both C++ and Java provides a powerful mechanism for building complex
software systems by promoting code reuse, modularity, and polymorphism.
16:- What is Applet? Write a program in Applet to Add two number and display values in Applet
on Button Click.
In Java, an applet is a small program that runs within a web browser or applet viewer. It is
primarily used to create dynamic and
interactive content on web pages. Applets are embedded in HTML documents and executed by
a web browser or applet viewer.
Here's a program in Java applet to add two numbers and display the result in an applet window
when a button is clicked:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AddApplet extends Applet implements ActionListener {
TextField num1Field, num2Field, resultField;
Button addButton;
public void init() {
num1Field = new TextField(10);
num2Field = new TextField(10);
resultField = new TextField(10);
resultField.setEditable(false); // Make the result field read-only
addButton = new Button("Add");
addButton.addActionListener(this); // Register action listener for button
add(new Label("Enter first number:"));
add(num1Field);
add(new Label("Enter second number:"));
add(num2Field);
add(addButton);
add(new Label("Result:"));
add(resultField);
}
public void actionPerformed(ActionEvent e) {
int num1 = Integer.parseInt(num1Field.getText());
int num2 = Integer.parseInt(num2Field.getText());
int result = num1 + num2;
resultField.setText(String.valueOf(result));
}
}
In this applet:
We create an applet class AddApplet that extends the Applet class and implements the
ActionListener interface to handle button click events.
In the init() method, we initialize the applet by creating text fields for entering numbers, a text
field for displaying the result, and a
button for performing addition. We add these components to the applet.
We register the applet as the action listener for the button using
addButton.addActionListener(this).
In the actionPerformed() method, we handle the button click event by retrieving the numbers
from the text fields, performing addition,
and displaying the result in the result field.