4th - Week 5
4th - Week 5
int main() {
MyClass myObj; // Create an object of MyClass (this will
call the constructor)
return 0;
}
Class Constructor
The following class have brand, model and year attributes, and a
constructor with different parameters. Inside the constructor we set
the attributes equal to the constructor parameters (brand=x, etc).
When we call the constructor (by creating an object of the class), we
pass parameters to the constructor, which will set the value of the
corresponding attributes to the same:
Class Constructor
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
brand = x;
model = y;
year = z;
}
};
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
Class Constructor
#include <iostream>
#include <string>
using namespace std;
class Car {
public:
// Constructor definition
Car(string brand, string model, int year) {
this->brand = brand;
this->model = model;
this->year = year;
}
// Function to print car information
void describe() {
cout << brand << " " << model << " " << year << endl;
}
private:
string brand;
string model;
int year;
};
int main() {
// Constructor is automatically called when an instance of Car is created
Car car1("Toyota", "Corolla", 2020);
// Printing car information
car1.describe(); // Output: Toyota Corolla 2020
return 0;
}
Class Methods
Constructors can also be defined outside the class. First, declare the
constructor inside the class, and then define it outside of the class by
specifying the name of the class, followed by the scope resolution ::
operator, followed by the name of the constructor (which is the same
as the class):
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z); // Constructor declaration
};
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Fruit {
public:
// Constructor declaration
Fruit(string name, string color);
// Function to describe fruit
void describe();
private:
string name;
string color;
};
// Constructor definition outside the class declaration
Fruit::Fruit(string name, string color) {
this->name = name;
this->color = color;
}
// Function definition to describe fruit
void Fruit::describe() {
cout << "Name: " << name << ", Color: " << color << endl;
}
int main() {
// Creating an instance of the Fruit class
Fruit apple("Apple", "Red");
// Describing the fruit
apple.describe(); // Output: Name: Apple, Color: Red
return 0;}
Class Methods
Methods are functions that belongs to the class. There are two ways
to define functions that belongs to a class:Inside / Outside class
int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
#include <iostream>
Class Methods
using namespace std;
class MyClass {
private:
int myVar;
public:
MyClass(int x) {
myVar = x;
}
int getMyVar() {
return myVar;
}
};
void myFunction() {
cout << "Hello, world!" << endl;
}
int main() {
MyClass obj(42);
cout << "My variable is: " << obj.getMyVar() << endl;
myFunction();
return 0;
}
Class Methods
This program defines a class called MyClass with a private member
variable called myVar and a public constructor that takes an integer
argument and sets myVar to that value. It also defines a public
member function called getMyVar that returns the value of myVar.
int main()
{
Student student1("John Doe", 20, "Computer Science"); // Creating an instance of the
Student class
student1.setGPA(3.5); // Setting the GPA
student1.printInfo(); // Printing student information
return 0;
}
Class Methods
MyClass::MyClass(int x) {
myVar = x;
}
int MyClass::getMyVar() {
return myVar;
}
void myFunction() {
cout << "Hello, world!" << endl;
}
int main() {
MyClass obj(42);
cout << "My variable is: " << obj.getMyVar() << endl;
myFunction();
return 0;
}
Class Methods
Note that the class definition still includes the declarations for the
constructor and getMyVar function, but their definitions are
moved outside the class definition. This is a common pattern in C+
+ programming, where larger classes may have many member
functions that are best defined outside the class definition to
improve code organization and readability.
Class Methods
Parameters
You can also add parameters:
#include <iostream>
using namespace std;
class Car {
public:
int speed(int maxSpeed);
};
int main() {
Car myObj; // Create an object of Car
cout << myObj.speed(200); // Call the method with an argument
return 0;
}
Access Specifiers
int main() {
MyClass myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
return 0;
}
Access Specifiers
class MyClass {
int x; // Private attribute
int y; // Private attribute
};
Access Specifiers
public:
void setNum(int num) {
privateNum = num;
}
void printNum() {
std::cout << "Private number is: " <<
privateNum << std::endl;
}
};
int main() {
MyClass myObj;
myObj.setNum(42);
myObj.printNum();
return 0;
}
Access Specifiers
class Animal {
protected:
int age;
public:
Animal(int a) : age(a) {}
void bark() {
std::cout << "Woof! I am " << age << " years old." << std::endl;
}
};
int main() {
Dog myDog(3);
myDog.bark();
myDog.speak();
return 0;
}
#include <iostream>
#include <string> Access Specifiers
using namespace std;
class Student {
public:
Student(string name, int age, string major); // Constructor declaration
void setGPA(double gpa); // Function to set the GPA
void printInfo(); // Function to print student information
protected:
string name;
int age;
string major;
double gpa;
};
// Constructor definition
Student::Student(string name, int age, string major) {
this->name = name;
this->age = age;
this->major = major;
this->gpa = 0.0; // Initializing GPA to 0
}
// Function to set the GPA Access Specifiers
void Student::setGPA(double gpa) {
this->gpa = gpa;
}
int main() {
Student student1("John Doe", 20, "Computer Science"); // Creating an instance of the
Student class
student1.setGPA(3.5); // Setting the GPA
student1.printInfo(); // Printing student information
return 0;
}