forked from natto1784/ooplab
4.1 KiB
4.1 KiB
- Write a program to take name, address as a character array, age as int , salary as float and contains inline functions to set the values and display it in class named person.
- Using the concept of function overloading.Write function for calculating the area of triangle ,circle and rectangle.
- Write a program to find number m to power n. The function power takes a double value for m and int value for n. Use default value for n to make the function to calculate squares when this argument is omitted.
Write a program to take name, address as a character array, age as int , salary as float and contains inline functions to set the values and display it in class named person.
#include <iostream>
#include <string.h>
class Person {
private:
static const std::size_t MAX_NAME = 30;
static const std::size_t MAX_ADDRESS = 30;
char name[MAX_NAME];
char address[MAX_ADDRESS];
unsigned int age;
float salary;
public:
void getName();
void getAddress();
void getAge();
void getSalary();
void setName(const char[30], size_t);
void setAddress(const char[100], size_t);
void setAge(unsigned int);
void setSalary(float);
};
inline void Person::getName() {
std::cout << name << std::endl;
}
inline void Person::getAddress() {
std::cout << address << std::endl;
}
inline void Person::getAge() {
std::cout << age << std::endl;
}
inline void Person::getSalary() {
std::cout << salary << std::endl;
}
inline void Person::setName(const char name[], size_t size) {
strncpy(this->name, name, (size <= MAX_NAME ? size : MAX_NAME - 1));
if (size > MAX_NAME)
this->name[MAX_NAME - 1] = '\0';
}
inline void Person::setAddress(const char address[], size_t size) {
strncpy(this->address, address, (size <= MAX_ADDRESS ? size : MAX_ADDRESS - 1));
if (size > MAX_NAME)
this->name[MAX_NAME - 1] = '\0';
}
inline void Person::setAge(unsigned int age) {
this->age = age;
}
inline void Person::setSalary(float salary) {
this->salary = salary;
}
int main() {
Person person;
const char name[] = "Retard Singh";
const char address[] = "420 RetardVille";
unsigned int age = 44;
float salary = 4.44;
person.setName(name, sizeof(name));
person.setAddress(address, sizeof(address));
person.setAge(age);
person.setSalary(salary);
person.getName();
person.getAddress();
person.getAge();
person.getSalary();
return 0;
}
Retard Singh
420 RetardVille
44
4.44
Using the concept of function overloading.Write function for calculating the area of triangle ,circle and rectangle.
#include <cmath>
#include <iostream>
// Triangle
double area(double a, double b, double c) {
double s = (a + b + c) / 2;
return std::sqrt(s * (s - a) * (s - b) * (s - c));
}
// Rectangle
double area(double a, double b) { return a * b; }
// Circle
double area(double a) { return M_PI * a * a; }
int main() {
double a = 3, b = 4, c = 5;
std::cout << "Area of a triangle with sides " << a << ", " << b << " and "
<< c << ": " << area(a, b, c) << std::endl
<< "Area of rectangle with sides " << a << " and " << b << ": "
<< area(a, b) << std::endl
<< "Area of circle with radius " << a << ": " << area(a)
<< std::endl;
return 0;
}
Area of a triangle with sides 3, 4 and 5: 6 Area of rectangle with sides 3 and 4: 12 Area of circle with radius 3: 28.2743
Write a program to find number m to power n. The function power takes a double value for m and int value for n. Use default value for n to make the function to calculate squares when this argument is omitted.
#include <cmath>
#include <iostream>
double uexponent(double m, unsigned int n) {
double ret;
if (!n)
return 1;
if (n == 1)
return m;
ret = uexponent(m, n / 2);
ret *= ret;
if (n % 2)
return ret * m;
else
return ret;
}
double exponent(double m, int n = 2) {
if (n >= 0)
return uexponent(m, n);
return 1 / uexponent(m, std::abs(n));
}
int main() {
double m = 4.1;
int n = 4;
std::cout << m << " raised to " << n << ": " << exponent(m, n) << std::endl
<< "When n is omitted: " << exponent(m) << std::endl;
}
4.1 raised to 4: 282.576 When n is omitted: 16.81