parajulirajesh.com.np

Rajesh Parajuli

Parameterized constructor

Example 1

#include <iostream>
using namespace std;
class SumCalculator {
public:
int number1;
int number2;

// Parameterized constructor with different parameter names
SumCalculator(int n1, int n2) {
number1 = n1;
number2 = n2;
}

// Method to display the numbers and their sum
void displaySum() {
int sum = number1 + number2;
cout << “Number 1: ” << number1;
cout << “\n Number 2:” << number2;
cout << “\n Sum: ” << sum;
}
};

int main() {
// Creating an object using the parameterized constructor
SumCalculator calculator(10, 20);

// Displaying the numbers and their sum
calculator.displaySum();

return 0;
}

Example 2

#include <iostream>
using namespace std;

class Person {
public:
string name;
int age;

// Parameterized constructor with different parameter names
Person(string n, int a) {
name = n;
age = a;
}

// Method to display person’s details
void displayDetails() {
cout << “Name: ” << name << endl;
cout << “Age: ” << age << endl;
}
};

int main() {
// Creating an object using the parameterized constructor
Person person1(“rajesh”, 27);

// Displaying the details of the person
person1.displayDetails();

return 0;
}

Exam Writing Tips:

  • Use / make Graph or picture to clear the concepts,
  • Prefer to write in points not in paragraph,
  • Write definition and make simple program on that particular questions with logic
  • If you cannot write program, write algorithm or syntax using structure of C++ program.

Important Topics

Define C++ and Features/characteristics of C++/Opps

What is object oriented Programming Language

  1. Class – Meaning and represent simple program using class
  2. Objects- Meaning and represent simple program using objects
  3. Encapsulation- Meaning and represent simple program using Encapsulation
  4. Abstraction- Meaning and represent simple program using abstraction
  5. Polymorphism Meaning and represent simple program using polymorphism
  6. Inheritance- Meaning and represent simple program using inheritence
Basic structure of C++ program
Data types in C++
difference between array and pointer
Meaning of dynamic memory and Dynamic memory allocation methods in C++
Defing Function, syntax of function, types of function call
Difference between Call by value and call by reference
Function Overloading
What is exception/error and Types of Error or what is exception handling in C++, 
Write a program to solve divide by zero exception handling by using try, throw and catch keywords.
Define template and its types with simple program example
Advantages of template
 

C++ (OOPS)

* Concept of Template

Template is a frame or blueprint which allow us to create generic classes and generic functions.  The keyword template is used to define function template and class template. In C++, Using Template keyword, we can work with different datatype with same functions and classes with same arguments and parameters, no need to write same code again and again for declaring different data type. C++ allows generic classes and functions to handle different data types.

Template can be represented in two ways:

 

Function template:  Function Template is used to define a function to handle arguments of different types in different times.

Example of Function Template

Write C++ program to find greatest number from two numbers by using template Function.

#include<iostream.h>
#include<conio.h>
using namespace std;
template <class X>
x big(x a, x b)
{
if(a>b)
return(a);
else
return(b);
}
int main()
{
cout<<big(4,5);
cout<<big(5.8,3.5);
getch();
}

*Function Overloading Templates

Example of Overloading Function Template

#include<iostream.h>
#include<conio.h>
using namespace std;
template <class T>
T Max(T a, T b)
{
if(a>b)
return a;
else
return b;
}
template <class T>
T Max(T a, T b, Tc)
{
if(a>b && a>c)
return a;
else if(b>a && b>c)
return b;
else
return c;
}
void main()
{
int a,b,c;
cout<<“Enter three numbers:”;
cin>>a>>b>>c;
cout<<“Maximum number among “<<a<<” and “<<b<<” is: “<<Max(a,b)<<end1;
cout<<“Maximum number among “<<a<<” and “<<b<<” and <<c<<” is: “<<Max(a,b,c)<<end1;
getch();
}
output:
Enter three numbers:1 4 65
Maximum number among 1 and 4 is:4
Maximum number among 1, 4 and 65 is:65

Paradigm can also be termed as method to solve some problem or do some task. Programming paradigm is an approach to solve problem using some programming language or also we can say it is a method to solve a problem using tools and techniques that are available to us following some approach. There are lots for programming language that are known but all of them need to follow some strategy when they are implemented and this methodology/strategy is paradigms. Apart from varieties of programming language there are lots of paradigms to fulfill each and every demand. They are discussed below:

 

1. Imperative programming paradigm: It is one of the oldest programming paradigm. It features close relation to machine architecture. It is based on Von Neumann architecture. It works by changing the program state through assignment statements. It performs step by step task by changing state. The main focus is on how to achieve the goal. The paradigm consist of several statements and after execution of all the result is stored

Advantages:

  1. Very simple to implement
  2. It contains loops, variables etc.

Disadvantage:  

  1. Complex problem cannot be solved
  2. Less efficient and less productive
  3. Parallel programming is not possible

Imperative programming is divided into three broad categories: Procedural, OOP and parallel processing. These paradigms are as follows:

 
  • Procedural programming paradigm – 
    This paradigm emphasizes on procedure in terms of under lying machine model. There is no difference in between procedural and imperative approach. It has the ability to reuse the code and it was boon at that time when it was in use because of its reusability.
  • Object oriented programming – 
    The program is written as a collection of classes and object which are meant for communication. The smallest and basic entity is object and all kind of computation is performed on the objects only. More emphasis is on data rather procedure. It can handle almost all kind of real life problems which are today in scenario.

Advantages: 

  • Data security
  • Inheritance
  • Code reusability
  • Flexible and abstraction is also present

Object-oriented analysis and design (OOAD) is a technical approach for analyzing and designing an application, system, or business by applying object-oriented programming, as well as using visual modeling throughout the software development process to guide stakeholder communication and product quality.

Scroll to Top