Join Regular Classroom : Visit ClassroomTech

Programming in C++ – condewindow.in

Related Topics

C++ Programing

write function without a return type in C++ ? If yes write the reason with example.

 A function without a return type in C++ as well. Such functions are declared with the void keyword as their return type.

Here’s an example:

#include <iostream>
using namespace std;

void print_hello() {
    cout << "Hello!" << endl;
}

int main() {
    print_hello();
    return 0;
}

In this example, we have defined a function called print_hello() which does not return anything (void). The function simply prints “Hello!” to the console when called. In the main() function, we call print_hello() and observe the output “Hello!”.

What is the usage of srand() and rand() function in C++ ?

The srand() and rand() functions are used to generate pseudo-random numbers in C++.

The srand() function is used to seed the random number generator with a starting value. If the same seed is used, the same sequence of random numbers will be generated. The syntax for srand() is as follows:

void srand(unsigned int seed);

The rand() function returns a random integer within a range determined by the implementation. The syntax for rand() is as follows:

int rand(void);

Here is an example program that uses srand() and rand() to generate a random number between 1 and 10:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main() {
    srand(time(NULL)); // seed the random number generator with the current time
    int random_number = rand() % 10 + 1; // generate a random number between 1 and 10
    cout << "The random number is: " << random_number << endl;
    return 0;
}

In this example, we first seed the random number generator using srand(time(NULL)). The time(NULL) function returns the current time as a time_t value, which is used as the seed. This ensures that a different sequence of random numbers is generated each time the program is run.

We then use the rand() function to generate a random integer between 0 and 9 using the modulus operator (%) and add 1 to get a random integer between 1 and 10. Finally, we output the random number to the console using cout.

What is Class and Object in C++ ?

In C++, a class is a user-defined data type that encapsulates data and functions that operate on that data. A class serves as a blueprint for creating objects, which are instances of the class.

In other words, a class defines a set of properties and behaviors that describe a particular entity or concept. For example, a Person class might have properties such as name, age, and address, as well as methods like get_name(), get_age(), and set_address().

Here’s an example of a simple Person class in C++:

#include <string>
using namespace std;

class Person {
private:
    string name;
    int age;
    string address;

public:
    Person(string n, int a, string addr) {
        name = n;
        age = a;
        address = addr;
    }

    string get_name() {
        return name;
    }

    int get_age() {
        return age;
    }

    void set_address(string addr) {
        address = addr;
    }
};

In this example, the Person class has three private data members (name, age, and address) and three public member functions (Person(), get_name(), and get_age(), and set_address()). The constructor Person() initializes the object with the provided values for name, age, and address.

An object, on the other hand, is an instance of a class. It is created from the blueprint provided by the class definition. To create an object of a class, we use the syntax:

ClassName objectName;

For example, to create a Person object:

Person person1("John Doe", 30, "123 Main St.");

In this example, person1 is an object of the Person class, initialized with the values "John Doe", 30, and "123 Main St.". We can now access the properties and methods of the person1 object using the dot (.) operator, for example:

cout << person1.get_name() << endl; // output: "John Doe"

What is local function and global function in C++?

In C++, a local function is a function that is defined within the scope of another function or block of code, and can only be accessed within that scope. A global function, on the other hand, is a function that is defined outside of any other function or block of code, and can be accessed from any part of the program.

Here’s an example of a local function:

#include <iostream>
using namespace std;

void outer_function() {
    void inner_function() {
        cout << "This is a local function." << endl;
    }

    inner_function();
}

int main() {
    outer_function();
    return 0;
}

In this example, inner_function() is defined within the scope of outer_function(), making it a local function. The inner_function() can only be accessed within outer_function(), and is not visible outside of that scope.

Here’s an example of a global function:

#include <iostream>
using namespace std;

void global_function() {
    cout << "This is a global function." << endl;
}

int main() {
    global_function();
    return 0;
}

In this example, global_function() is defined outside of any other function or block of code, making it a global function. The global_function() can be accessed from anywhere in the program, including from the main() function.

It’s worth noting that local functions are not part of the C++ standard, and are not supported by all compilers. Some compilers may allow local functions as an extension to the language, but it’s generally recommended to avoid using them to ensure maximum portability of your code.

Top Company Questions

Automata Fixing And More

      

We Love to Support you

Go through our study material. Your Job is awaiting.

Recent Posts
Categories