class rectangle
{
public:
void area(int l, int b)
{
cout<<"area="<< a;
}
};
void main()
{
rectangle r1;
r1.area(10,5); //example of message passing
}
C/C++ Programming Solutions, Notes , Old Question Solutions, SLC/SEE Preparation, University Board Exam, Old Question Solutions. PSC preparation for Computer Engineering, computer officer, computer Programmer level-7.
class rectangle
{
public:
void area(int l, int b)
{
cout<<"area="<< a;
}
};
void main()
{
rectangle r1;
r1.area(10,5); //example of message passing
}
We know that structure is a collection of heterogeneous data elements under a common name. It is a convenient tool for handling a group of logically related data items. It is a user-defined data type with a template that serves to define its data properties. Although structure is a user-defined data type, but it has some limitations.
Class is a collection of data members and member function bundled together as a unit. Using class, encapsulation as well as data hiding has been possible. Similarly, inheritance and polymorphism has been possible due to class. So, it is the more advanced data structure than the structure of C and it has been removed the drawbacks of the structure. The main drawbacks of structure of Care as follows:
{
float real;
float imaginary;
};
struct c1,c2,c3;
c3=c1+c2;
The member variable of structure 'complex' can easily be assigned values using the dot(.) operator, but we cannot add two complex numbers or subtract one from the other. So, c3=c1+c2 or c3=c1-c2 both are illegal in C.
But, in C++, we can design a complex class and add or subtract them using + or- operator by the help of polymorphism. For example:
class complex
{
private:
float real;
float imaginary;
public:
complex operator+(operator);
complex(float, float);
}
Complex c1.c2,c3;
C3=c1+c2;
For example:
The data members of the structure can be easily accessed by the structure variable in any part of the program such as c1.real, c1.imaginary even from the 'main()' function but the data members of the class cannot be accessed directly outside their member function. So, c1.real or c1.imaginary is not allowed directly in the 'main()' function of the program.
What makes the OOP better than POP? Explain with features of OOP.
Solution:
POP stands for 'Procedural Oriented Programming' which is the conventional programming approach. In POP, the entire program is viewed as a sequence of things to be done such as taking input, calculating and printing and the entire program is divided into smaller programs known as function and it gives more emphasis to function.
Object-oriented programming is the most recent concept in computer programming paradigms and it has been defined as "an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand".
Being the recent programming approach, OOP offers several benefits than POP approach and some of the most important benefits are as follows:
We know that structure is a collection of heterogeneous data elements under a common name. It is a convenient tool for handling a group of logically related data items. It is a user-defined data type with a template that serves to define its data properties. Although structure is a user-defined data type, but it has some limitations.
Class is a collection of data members and member function bundled together as a unit. Using class, encapsulation as well as data hiding has been possible. Similarly, inheritance and polymorphism has been possible due to class. So, it is the more advanced data structure than the structure of C and it has been removed the drawbacks of the structure. The main drawbacks of structure of Care as follows:
{
float real;
float imaginary;
};
struct c1,c2,c3;
c3=c1+c2;
The member variable of structure 'complex' can easily be assigned values using the dot(.) operator, but we cannot add two complex numbers or subtract one from the other. So, c3=c1+c2 or c3=c1-c2 both are illegal in C.
But, in C++, we can design a complex class and add or subtract them using + or- operator by the help of polymorphism. For example:
class complex
{
private:
float real;
float imaginary;
public:
complex operator+(operator);
complex(float, float);
}
Complex c1.c2,c3;
C3=c1+c2;
For example:
The data members of the structure can be easily accessed by the structure variable in any part of the program such as c1.real, c1.imaginary even from the 'main()' function but the data members of the class cannot be accessed directly outside their member function. So, c1.real or c1.imaginary is not allowed directly in the 'main()' function of the program.
{
public:
void area(int l, int b)
{
cout<<"area="<< a;
}
};
void main()
{
rectangle r1;
r1.area(10,5); //example of message passing
}
syntax:
class class_name
{
private:
data member;
member function;
public:
data member;
member function;
}; //end of class
Generally data are placed in private section and function are placed in public section.
An object is an instance of a class.
For example, the Car
class defines the model, brand, and mileage. Now, based on the definition, we can create objects like
Car suv;
Car sedan;
Car van;
Here, suv, sedan, and van are objects of the Car
class. Hence, the basic syntax for creating objects is:
Class_Name object_name;
function overloading means using same function name to perform variety of task. But in function overloading we must consider two things. They are:
Note: return type has no role in function overloading
int sum(int a, int b)
{
return (a+b);
}
int sum(int a1, int b1, int c1)
{
return(a1+b1+c1);
}
int sum(int d)
{
return (d+5);
}
void main()
{
cout<<"The sum of one variable"<<sum(10)<<endl;
cout<<"The sum of two variable"<<sum(10,20)<<endl;
cout<<"The sum of three vriable"<<sum(10,20,30)<<endl;
getch();
}
type II
#include<iostream.h>
#include<conio.h>
using namespace std;
int area(int l, int b)
{
return (l*b)
}
float area(int b, float h)
{
return (0.5*b*h);
}
void main()
{
int l1,b1;
float h;
cout<<"Enter the value of l1,b1 & h1<<endl;
cin>>l1>>b1>>h1;
cout<<"The area of rectangle is <<area(l1,b1)<<endl;
cout<<"The area of triangle is<<area(l1,h1);
getch();
}
Inline function:
It is the function that is expanded inline when it is called with corresponding code. The syntax for making inline is inline return-type function-name(no arguments/arguments)
Note:- inline function should always have return type.