Object-Oriented Programming is a programming language that is commonly used in the current software development industry. OOP is preferred because it deals with objects and has extra features such as inheritance, encapsulation, data hiding, and polymorphism.
All these programming methods revolve around objects. In this approach, the programmer considers the real entities involved in the plan. Everything in this programming method is done solely with objects.
Class:
A class is an example of an object, and a class is a template that can be used to define an object. [Object-Oriented Programming with Turbo C++]
A class comprises member variables and methods. In object-oriented programming, inheritance and encapsulation are properties of a class.
Class classname
{ member variables, methods };
Encapsulation is a process used to bind the data and operations necessary to manipulate the data. The main characteristic of encapsulation is to provide data hiding and combine the data and functions.
Constructor:
Frequently, to initialize some or all of the member variables for an object, you declare the object. C++ has special provisions for such initializations.
A constructor is a member function of a class that has the same name as the class. A constructor is called automatically when an object of the class is declared.
Syntax
Classname () ;
It is the declaration of the constructor. It is declared in the class itself.
Syntax
Classname(datatype variablename) ;
Arguments can be passed in the constructor.
Syntax
Classname(datatype variablename) : initialization ;
Initialization is done at the time of constructor declaration itself.
Constructors are used to initialize objects. A constructor must have the same name as the class of which it is a member. A constructor definition cannot return a value. The code for a constructor and destructor can be defined either in or out of a class body like member functions. If no constructor is defined, a default constructor is created by the compiler without any parameters. If we pass parameters to a constructor, then no default constructor is created.
DEFAULT CONSTRUCTORS:
A constructor that has no parameters is called a default constructor. If parameters are used in the default constructor, then all the parameters have default values.
Constructor()
{
Name = “”;
Size = 0;
Text= “”;
}
It takes 0 parameters and initializes them.
An initializer list can be used in constructors to set the values before the body of the constructor is entered.
DESTRUCTOR:
A destructor is a member function declared the same as a constructor with the name of the class. The name of the destructor is preceded by a “~” symbol. The destructor deallocates the memory used by the constructor. If the destructor is not defined by the user, then the compiler creates a default destructor automatically.
<classname>::~<class name>(void)
{ // destructor body }
ACCESS SPECIFIERS:
Access specifiers determine the functionality to call that follows it. It can be accessed until the following specifier is declared. In general, most programs use only public and private access specifiers; protected is not used often.
Public: The members of a class that are declared with the keyword “public” can be accessed from anywhere in the program. These members work like global variables for that program.
Syntax:
public:
member data;
member functions();
DEFAULT CONSTRUCTORS:
A builder which has no parameters is called DEFAULT CONSTRUCTORS. If parameters are used in the default builder, then all the parameters have default values.
Constructor ( ) { Name = “”; Size = 0; Text = “”; }
It takes 0 parameters and initializes them.
Initializer list can be used in builders to put the values before the organic structure of the builder is entered.
DESTRUCTOR:
Destructor is a member map declared with the same name as the builder, preceded by a ~ symbol. Destructor deallocates the memory which is used by the builder. If the destructor has not been defined by the user, then the compiler creates a default destructor automatically.
<classname>::~<category name> () { // destructor organic structure }
ACCESS SPECIFIERS:
Access specifiers determine the functionality to call that follows it. It can be accessed until the following specifier is declared. In general, most of the programs use only public and private access specifiers; protected is not used often.
Public: The members of a category which are declared with a keyword public can be accessed from anywhere in the program. These members work like global variables to that program.
Syntax:
Public: Member informations; Member maps ();
Private: The members of a category which are declared with the keyword private can be accessed within the category. These members work like local members.
Syntax:
Private: Member informations; Member maps ();
Protected: The members of a category which are declared with the keyword protected can be accessed as private access specifier with the following condition. The derived category cannot access the members declared under the protected specifier.
Syntax:
Protected: Member informations;
COPY CONSTRUCTOR:
A copy builder is used to make a copy of an existing object of a category from another category object with the same category type. We can’t use the copy builder with parameters of the same type of category. The copy builder can be provided with excess statements as they have default parameters.
If there doesn’t exist a user-defined copy constructor for a category, then the compiler will make a copy constructor itself, giving public access to that copy constructor. There is no necessity to make a category if the category has an inaccessible copy constructor.
DATA MEMBERS:
Data members are not data types. It’s merely a logical categorization available in C++ for the type of members. These data members can be of any fundamental data types in C++, it can be a struct type, and so on. Data members can be declared either as arrows and can be accessed usually as other data members. Data members use access specifiers (such as public, private, protected) to be assigned to the category.
Syntax:
Data type variable1, variable2, … … … … .. ;
MEMBER FUNCTIONS:
The operators or maps which are declared as members in a category are called member maps. These maps don’t include operators or maps which are declared with a friend specifier. Member maps declared with static are called static member maps. Member maps which are not declared with static are called non-static member functions.
Member maps are defined within the range of the category. Member map organic structure analyzed after the category declaration, so we can use the members of that category in the member map organic structure.
Syntax:
Functionname () { statements }
Member informations can be used anywhere in the program depending on the access specifier used to declare the member informations. Member maps can be called from other member maps in a category. While making this, the object and the member selection operator need not be used. The member map can be declared as constant with the aid of the keyword ‘const’.
Syntax:
Const Functionname (parameters) const { statements }
Assignment Operators:
The assignment operators are used to assign values to variables. This can be done using the symbol ‘=’. There are different types of assignment operators such as +=, *=, /=, <=, >=, and so on. Whenever we assign a value to a variable, the compiler gives the statement to the right side of the variable.
FRIEND FUNCTION:
By using the keyword “friend” in front of a class, we can access private data, member functions, and data from other classes. The word “friend” is mentioned only in the declaration part, but not the definition part. A function can be declared as a friend in a number of classes. We can use private and public access specifiers to declare a friend function. These friend functions can be called without using an object. They have statements as objects.
Syntax:
friend classname()
{ member functions…;
variables…;
}
AGGREGATE:
An aggregate is a type of composition where there is no relationship between complex objects and other subobjects. These are implemented identically. To create an aggregate class, we can add other subclasses as member variables.
Use pointers to variables that point to objects outside, whose scope is out of the aggregate class.
It is not involved in canceling and creating subclasses.
DYNAMIC MEMORY ALLOCATION USING NEW/DELETE:
The “new” operator is used to allocate dynamic memory for objects from the free space called free store. The “delete” operator is used to deallocate the dynamic memory used for the object. First, the memory which is allocated for the component is deleted, and then memory allocated for an array is deleted. We can deallocate the memory block which is created using the “new” operator or void pointer.
Syntax:
new variable;
delete variable;
POINTERS:
A pointer variable is a variable that stores the memory reference of the variable. We can also access the reference of the variable. A pointer variable is declared by the ‘*’ symbol. The reference of the pointer variable is called with the aid of the ‘&’ symbol.
Syntax:
datatype *variablename;
Conclusion:
This assignment covers the key concepts of C++ with the help of sentence structures and programs. The output of the compiled programs is shown.
Reference:
*C++ The Complete Reference by Herbert Schildt, Second Edition.
Title: List of Resources for C++ Programming
- Problem Solving with C++ by Walter Savitch, Sixth Edition
- Absolute C++ by Walter Savitch, Third Edition
- Hypertext Transfer Protocol: //publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr375.htm
- Hypertext Transfer Protocol: //www.nusphere.com/kb/phpmanual/ref.objaggregation.htm
- Hypertext Transfer Protocol: //publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr392.htm
- Hypertext Transfer Protocol: //en.wikipedia.org/wiki/Destructor_(computing)
- Hypertext Transfer Protocol: //www.codersource.net/cpp_tutorial_class.html
- www.msdn.com
- www.cplusplus.com
Appendix:
#include
#include
using namespace std;
class cmplx {
float a, b;
public:
void setdata(float realvl, float imagvl) {
cout << “enter the existent value ” << endl; cin >> realvl;
a = realvl;
cout << “enter fanciful value ” << endl; cin >> imagvl;
b = imagvl;
}
friend cmplx add(cmplx d1, cmplx d2);
void display() {
cout << “the cmplx no is ” << a << “+i” << b << endl;
}
};
cmplx add(cmplx d1, cmplx d2) {
cmplx d3;
d3.a = d1.a + d2.a;
d3.b = d1.b + d2.b;
return d3;
}
class con1 {
public:
con1() {
cout << “Default builder” << endl;
}
con1(const con1& A) {
cout << “Copy builder” << endl;
}
con1& operator=(const con1& A) {
cout << “Operator” << endl;
return *this;
}
};
class centimeter {
protected:
con1 Ob;
public:
centimeter() {
cout << “builder” << endl;
}
#ifndef Good
centimeter(const centimeter& A; R) {
cout << “transcript cconstructor” << endl;
*this = r;
}
#else
#endif
centimeter& operator=(const centimeter& A; R) {
cout << “Operator” << endl;
ob = r.ob;
return *this;
}
};
void main() {
float a = 0.0;
float b = 0.0;
cmplx d1, d2, d3;
d1.setdata(a, b);
d2.setdata(a, b);
d3 = add(d1, d2);
d3.display();
cout << “case of default builder” << endl;
centimeter ob1;
cout << “case of transcript builder” << endl;
centimeter ob2(ob1);
ob2 = ob1;
system(“pause”);
}
End product
Code Explanation:
In the above program, constructors, destructors, and other features are used, and the addition of complex numbers is calculated.
The class named cmplx is taken, which has data members of float data type and few public member functions. In the member functions, we see a friend function that accesses the data of the class. There are two more classes named con1 and centimeter, which have public and protected access specifiers, default constructor, copy constructor, and assignment operator.
The addition of two complex numbers is calculated using the add function. The program takes input from the user regarding the real and imaginary values, adds the real and imaginary parts separately, and displays the output as a new complex number. It also displays information about the default constructor and copy constructor invoked while executing the program.
Here we see functions declared and defined at the same place. That is, the functions declared in the class are defined there in the class.
void show()
{
cout << “the cmplx no is ” << a << ” + i ” << b << endl;
}
The show function is defined and declared in the class itself. When functions are defined like this, we say that they are implicit inline functions. We can also declare them explicitly by placing the keyword “explicit” in the front of the definition and declaration.
We also used the keyword ‘this’. While using a copy constructor, we use the word “const” in the constructor declaration. All the access specifiers are used.
The output of the above program is given as a screenshot in the window above.