C++

String Handling functions in C++

Posted on Updated on

String Handling functions in C++

Example of strlen()

#include <stdio.h>

#include <string.h>

void main(){

char a[20]=”Program”;

char b[20]={‘P’,’r’,’o’,’g’,’r’,’a’,’m’,”};

char c[20];

printf(“Enter string: “);

gets(c);

printf(“Length of string a=%d \n”,strlen(a));

//calculates the length of string before null charcter.

printf(“Length of string b=%d \n”,strlen(b));

printf(“Length of string c=%d \n”,strlen(c));

}

Output

Enter string: String

Length of string a=7

Length of string b=7

Length of string c=6

Example of strcpy()
#include <stdio.h>
#include <string.h>
void main(){
    char a[10],b[10];
    printf("Enter string: ");
    gets(a);
    strcpy(b,a);   //Content of string a is copied to string b.
    printf("Copied string: ");
    puts(b);
}

Output

Enter string: Programming Tutorial
Copied string: Programming Tutorial
Example of strcat()
#include <stdio.h>
#include <string.h>
Void main(){
    char str1[]="This is ", str2[]="programiz.com";
    strcat(str1,str2);   //concatenates str1 and str2 and resultant string is stored in str1.
    puts(str1);    
    puts(str2); 
}

Output

This is programiz.com
programiz.com
Example of strcmp()
#include <stdio.h>
#include <string.h>
int main(){
  char str1[30],str2[30];
  printf("Enter first string: ");
  gets(str1);
  printf("Enter second string: ");
  gets(str2);
  if(strcmp(str1,str2)==0)
      printf("Both strings are equal");
  else
      printf("Strings are unequal");
  return 0;
}

Output

Enter first string: Apple     
Enter second string: Apple
Both strings are equal.

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Advertisements

COMPOSITION AND INHERITANCE

Posted on Updated on

  • In real-life, complex objects are often built from smaller, simpler objects. For example, a car is built using a metal frame, an engine, some tires, a transmission, a steering wheel, and a large number of other parts.
  • A PC is built from a CPU, a motherboard, some memory, etc. This process of building complex objects from simpler ones is called composition (also known as object composition).
  • Composition is used for objects that have a has-a relationship to each other.
  • A car has-a metal frame, has-an engine, and has-a
  • A personal computer has-a CPU, a motherboard, and other components.
  • Composition is nothing but relationships between the different objects. Sometimes Object made up from another objects like Airplane is Wings, Landing gears, engines etc. this relationship is called composition
  • Inheritance is extends one class to another class like

Public class A

{//Here methods and variable etc.}

public class B:A

{//here methods and properties variable etc}

public static void main()

{B b = new B();

b.method();

b.Variable ..}

 INHERITANCE

  • One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application.
  • This also provides an opportunity to reuse the code functionality and fast implementation time.
  • When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived
  • The idea of inheritance implements the is a For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well

            Base & Derived Classes:

  • A class can be derived from more than one class, which means it can inherit data and functions from multiple base classes.
  • To define a derived class, we use a class derivation list to specify the base class. A class derivation list names one or more base classes and has the form:

class derived-class: access-specifier base-class

  • Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.
  • Consider a base class Shape and its derived class Rectangle as follows:

#include <iostream.h>
// Base class
class Shape
{ public:
voidsetWidth(int w)
{ width = w; }
voidsetHeight(int h)
{ height = h; }
protected:
int width;
int height;};
// Derived class
class Rectangle: public Shape
{ public:
intgetArea()
{ return (width * height); }};
void main()
{ Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout<< “Total area: ” <<Rect.getArea() <<endl;}

      When the above code is compiled and executed, it produces the following result:

 Total area: 35

 Total paint cost: $2450

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

THE THIS POINTER

Posted on

THE THIS POINTER

  • Every object in C++ has access to its own address through an important pointer called this pointer.
  • This pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.
  • The following example to understand the concept of this pointer:

#include <iostream.h>
class Box
{
public:
// Constructor definition
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<“Constructor called.” << endl;
length = l;
breadth = b;
height = h; }
double Volume()
{
return length * breadth * height; }
int compare(Box box)
{
return this->Volume() > box.Volume();
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box};
void main()
{ Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if(Box1.compare(Box2))
{
cout << “Box2 is smaller than Box1” <<endl;
}
else
{
cout << “Box2 is equal to or larger than Box1” <<endl;
}}

When the above code is compiled and executed, it produces the following result:

Constructor called.

Constructor called.

Box2 is equal to or larger than Box1

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

 

OOP in C++

Posted on

THE CLASS CONSTRUCTOR

  • A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
  • A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.

 

Following example explains the concept of constructor:

 

 

#include <iostream>
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor

private:
double length;
};

// Member functions definitions including constructor
Line::Line(void)
{
cout << “Object is being created” << endl;
}

void Line::setLength( double len )
{
length = len;
}

double Line::getLength( void )
{
return length;
}
// Main function for the program
void main( )
{
Line line;
// set line length
line.setLength(6.0);
cout << “Length of line : ” << line.getLength() <<endl;
}

When the above code is compiled and executed, it produces the following result:

 

 

12

Parameterized Constructor:

  • A default constructor does not have any parameter, but if you need, a constructor can have parameters. This helps you to assign initial value to an object at the time of its creation as shown in the following example:

#include <iostream>
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(double len); // This is the constructor

private:
double length;
};

// Member functions definitions including constructor
Line::Line( double len)
{
cout << “Object is being created, length = ” << len << endl;
length = len;
}

void Line::setLength( double len )
{
length = len;
}

double Line::getLength( void )
{
return length;
}
// Main function for the program
Void main( )
{
Line line(10.0);

// get initially set length.
cout << “Length of line : ” << line.getLength() <<endl;
// set line length again
line.setLength(6.0);
cout << “Length of line : ” << line.getLength() <<endl;
}

When the above code is compiled and executed, it produces the following result:

4+

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

OOP in C++

Posted on

OBJECT ORIENTED PROGRAMMING

 

  • OOPstands for Object Oriented Programming. This is a technique used to develop programs revolving around the real world entities. In OOPs programming model, programs are developed around data rather than actions and logics.
  • In OOPs, every real life object has properties and behavior, which is achieved through the class and object creation. They contain properties (variables of some type) and behavior (methods). OOPs provide a better flexibility and compatibility for developing large applications.
  • Reusability of Code:  In object oriented programming one class can easily copied to another application if we want to use its functionality .E.g.  When we work with hospital application and railway reservation application .In both application we need person class  so we have to write only one time the person class  and it can easily use in other application.
  • Easily Discover a bug: When we work with procedural programming it take a lot of time to discover a bug and resolve it .But in object   Oriented Programming   due to modularity of classes we can easily discover the bug and we have to change in one class only and this change make in all the application only by changing it one class only.
  • Object- Oriented Programming enables us to easily model our applications based on the real world which help us to easily identify the requirements, what code is to be written and how different classes interact with each other which have same properties and behaviors.

APPLICATIONS OF OOPS

  • OOP has become one of the programming buzz words today. There appears to be a great deal of excitement and interest among software engineers in using OOP.
  • Applications of OOP are beginning to gain importance in many areas. The most popular application of object-oriented programming, up to now, has been in the area of user interface design such as window. Hundreds of windowing systems have been developed, using the OOP techniques. Real-business system are often much more complex and contain many more objects with complicated attributes and method. OOP is useful in these types of application because it can simplify a complex problem. The promising areas of application of OOP include:
  •   Real-time system
  •   Simulation and modeling
  •   Object-oriented data bases
  •   Hypertext, Hypermedia, and expertext
  •   AI and expert systems
  •   Neural networks and parallel programming
  •   Decision support and office automation systems
  •   CIM/CAM/CAD systems

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

 

Classes and Objects in C++

Posted on

Introduction to Classes and Objects

 

  •  The classes are the important feature of C++ that leads to Object  Oriented Programming (OOP).
  •  Class is user defined data type, which holds its own data  members(member function and member variable), which can be  accessed and used by creating instance of that class
  •  Object – Objects have states and behaviors.
  •  Example:  A  dog  has  states  –  color,  name,  breed  as  well  as  behaviors – wagging, barking, eating. An object is an instance of a  class.
  •  Class – A class can be defined as a template/blueprint that describes  the behaviors/states that object of its type support.
  •  Methods – A method is basically a behavior. A class can contain  many methods. It is in methods where the logics are written, data is  manipulated and all the actions are executed.

Instant Variables – Each object has its unique set of instant variables. An object’s state is created by the values assigned to these instant variables.

 

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

 

Friend Function

Posted on

#include<iostream.h>

#include<conio.h>

class greater

{

private:

int no1,no2,no3;

public :

greater()

{

no1=4;

no2=7;

no3=1;

}

greater(int x,int y,int z)

{

no1=x;

no2=y;

no3=z;

}

friend int larger(greater &);

};

int larger(greater &g)

{

if(g.no1>g.no2 && g.no1>g.no3)

return g.no1;

else if(g.no2>g.no1 && g.no2>g.no3)

return g.no2;

else

return g.no3;

}

void main()

{

greater g(45,66,56);

cout<<“greater no is “<<larger(g);

 

getch();

}

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com