0% found this document useful (0 votes)
1 views12 pages

Method

The document explains the use of setter and getter methods in classes to modify and retrieve private variables in programming. It provides several examples in C++ demonstrating how to implement these methods in different classes such as Student, Employee, Bank, and others. Additionally, it includes an explanation of constructors, showcasing default, parameterized, and copy constructors with examples.

Uploaded by

yogeesh2006r
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views12 pages

Method

The document explains the use of setter and getter methods in classes to modify and retrieve private variables in programming. It provides several examples in C++ demonstrating how to implement these methods in different classes such as Student, Employee, Bank, and others. Additionally, it includes an explanation of constructors, showcasing default, parameterized, and copy constructors with examples.

Uploaded by

yogeesh2006r
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Set()

setter method is used to modify (set/update) the value of a private


variable. A method that sets or updates the value of a class attribute.
class Student {
private int age;

public void setAge(int age) {


[Link] = age;
}
}

get()

getter method is used to retrieve (get/read) the value of a private


variable. A method that returns the value of a class attribute.
class Student {
private int age;

public int getAge() {


return age;
}
}
Programs
#include <iostream>
using namespace std;

class Employee {
private:
int id;
float salary;

public:
void setId(int i) { id = i; }
void setSalary(float s) { salary = s; }

int getId() { return id; }


float getSalary() { return salary; }
};

int main() {
Employee e;
[Link](101);
[Link](50000);

cout << "ID: " << [Link]() << endl;


cout << "Salary: " << [Link]() << endl;
}
Program-2
#include <iostream>
using namespace std;

class Bank {
private:
double balance;

public:
void setBalance(double b) { balance = b; }
double getBalance() { return balance; }
};

int main() {
Bank b;
[Link](1000.75);

cout << "Balance: " << [Link]();


}
Program-3
#include <iostream>
using namespace std;

class Rectangle {
private:
int length, width;

public:
void setValues(int l, int w) {
length = l;
width = w;
}

int getArea() {
return length * width;
}
};

int main() {
Rectangle r;
[Link](5, 4);

cout << "Area: " << [Link]();


}
Program-4
#include <iostream>
using namespace std;

class Car {
private:
int speed;

public:
void setSpeed(int s) { speed = s; }
int getSpeed() { return speed; }
};

int main() {
Car c;
[Link](120);

cout << "Speed: " << [Link]();


}
Program-5
#include <iostream>
using namespace std;

class Book {
private:
float price;

public:
void setPrice(float p) { price = p; }
float getPrice() { return price; }
};

int main() {
Book b;
[Link](299.99);

cout << "Price: " << [Link]();


}
Program-6
#include <iostream>
using namespace std;

class Temperature {
private:
float temp;

public:
void setTemp(float t) { temp = t; }
float getTemp() { return temp; }
};

int main() {
Temperature t;
[Link](36.5);

cout << "Temperature: " << [Link]();


}
Program-7
#include <iostream>
using namespace std;

class Circle {
private:
int radius;

public:
void setRadius(int r) { radius = r; }
int getRadius() { return radius; }
};

int main() {
Circle c;
[Link](7);

cout << "Radius: " << [Link]();


}
Program-8
#include <iostream>
using namespace std;

class Marks {
private:
int marks;

public:
void setMarks(int m) { marks = m; }
int getMarks() { return marks; }
};

int main() {
Marks m;
[Link](90);

cout << "Marks: " << [Link]();


}
Program-9
#include <iostream>
using namespace std;

class Product {
private:
float price;

public:
void setPrice(float p) { price = p; }
float getPrice() { return price; }
};

int main() {
Product p;
[Link](150.5);

cout << "Product Price: " << [Link]();


}
Constructor
#include <iostream>
using namespace std;

class Demo {
public:
int x;

// 1. Default Constructor
Demo() {
x = 0;
cout << "Default Constructor called. x = " << x << endl;
}

// 2. Parameterized Constructor
Demo(int a) {
x = a;
cout << "Parameterized Constructor called. x = " << x << endl;
}

// 3. Copy Constructor
Demo(const Demo &obj) {
x = obj.x;
cout << "Copy Constructor called. x = " << x << endl;
}
};

int main() {
// Default constructor
Demo obj1;

// Parameterized constructor
Demo obj2(10);

// Copy constructor
Demo obj3 = obj2;

return 0;
}

You might also like