17.9.15

How to find the smallest string in C++

Write a function  min that has three C string parameters and returns the smallest.

Solution
char* min(char* a, char* b, char* c)
{
    return strcmp(a,b) < 0 ? strcmp(a,c) < 0 ? a : c : strcmp(b,c) < 0 ? b : c;
}

1 comment:


  1. // Taslima akter
    Id:201720079
    Batch:55 //
    Ans to ques number 1

    Types Of Constructor
    • Default Constructor
    • Parameterized Constructor
    • Copy Constructor

    Parameterized Constructor
    If a Constructor has parameters, it is called a Parameterized Constructor. Parameterized Constructors assist in initializing values when an object is created.
    General Syntax of Parameterized Constructor
    class class_name {
    public:
    class_name(parameters...) {
    // Constructor code
    }
    //... other Variables & Functions
    }


    Copy Constructor
    A copy constructor is a like a normal parameterized Constructor, but which parameter is the same class object. copy constructor uses to initialize an object using another object of the same class.
    General form of Copy Constructor
    class class_name {
    public:
    class_name(class_name & obj) {
    // obj is same class another object
    // Copy Constructor code
    }
    //... other Variables & Functions
    }
    Declaration in Main
    class_name object1(params);
    Copy Constructor - Method 1
    class_name object2(object1);
    Copy Constructor - Method 2
    class_name object3 = object1;


    Default Constructor
    A default constructor does not have any parameters or if it has parameters, all the parameters have default values.
    Syntax
    class class_name {
    public:
    // Default constructor with no arguments
    class_name();
    // Other Members Declaration
    }


    Ans to the ques num 2
    The use of function overloading:

    function overloading is essential to allow the function name (for example the constructor) to be used with different argument types.

    the use of function overloading is to save the memory space,consistency and readabiliy.



    #include
    Using namespace std;

    class CalculateArea
    {

    public:
    void Area(int r) //Overloaded Function 1
    {
    cout<<"\n\tArea of Circle is : "<<3.14*r*r;
    }

    void Area(int l,int b) //Overloaded Function 2
    {
    cout<<"\n\tArea of Rectangle is : "<
    using namespace std;

    class Shape {
    protected:
    int width, height;
    public:
    Shape( int a=0, int b=0)
    {
    width = a;
    height = b;
    }
    int area()
    {
    cout << "Parent class area :" <area();
    // store the address of Triangle
    shape = &tri;
    // call triangle area.
    shape->area();

    return 0;
    }


    When the above code is compiled and executed, Output Will Be :
    Example
    Parent class area
    Parent class area


    The reason for the incorrect output is that the call of the function area() is being set once by the compiler as the version defined in the base class. This is called static resolution of the function call, or static linkage - the function call is fixed before the program is executed. This is also sometimes called early binding because the area() function is set during the compilation of the program.



    ReplyDelete

Comment Here