Search
 
SCRIPT & CODE EXAMPLE
 

CPP

separating class into header and cpp file

//header file(.h)
//Implement function headers
#pragma once

class Rectangle
{
private:
	float length;
	float width;
public:
	//constructor
	Rectangle();

	//setters
	void setLength(float l);
	void setWidth(float w);

	//getters
	float getLength();
	float getWidth();

	//Calculating area
	float area(float length, float width);

	//print
	void print();
};

//(.cpp) file
//Function definations
#include <iostream>
#include "Rectangle.h"

using namespace std;

//constructor
Rectangle::Rectangle() {
	length = width = 0;
}

//setters
void Rectangle::setLength(float l) {
	length = l;
}
void Rectangle::setWidth(float w) {
	width = w;
}

//getters
float Rectangle::getLength() {
	return length;
}
float Rectangle::getWidth() {
	return width;
}

//Calculating area
float Rectangle::area(float length, float width) {
	float a = (float)length * width;
	return a;
}
 //print
void Rectangle::print() {
	cout << "Length= " << getLength() << endl;
	cout << "Width= " << getWidth() << endl;
	cout << "Area= " << area(length, width) << endl;
}

//main .cpp file
#include <iostream>
#include "Rectangle.h"

using namespace std;

int main() {
	float length, width;
	Rectangle rectangle;
	cout << "Enter length of the rectangle: ";
	cin >> length;
	cout << "Enter width of the rectangle: ";
	cin >> width;
	rectangle.setLength(length);
	rectangle.setWidth(width);
	rectangle.print();
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: remove at index vector c++ 
Cpp :: how to print fixed places after decimal point in c++ 
Cpp :: c++ vector iterator 
Cpp :: swap values in array c++ 
Cpp :: insertion sort c++ 
Cpp :: C++ mutex lock/unlock 
Cpp :: string to integer convert c++ 
Cpp :: c++ print number not in scientific notation 
Cpp :: dynamically generating 2d array in cpp 
Cpp :: string input with space c++ stl 
Cpp :: optimized bubble sort 
Cpp :: std distance c++ 
Cpp :: how to convert int to string c++ 
Cpp :: qt disable resizing window 
Cpp :: c++ iterate over vector 
Cpp :: c++ find element in vector 
Cpp :: how to use decrement operator in c++ 
Cpp :: c++ constructors 
Cpp :: struct and array in c++ 
Cpp :: number of words in c++ files 
Cpp :: terminal compile c++ 
Cpp :: memset in c++ 
Cpp :: create file c++ 
Cpp :: convert integer to string c++ 
Cpp :: min element in stl c++ 
Cpp :: struct and pointer c++ 
Cpp :: naive pattern matching algorithm 
Cpp :: cpp loop through object 
Cpp :: continue statement in c++ program 
Cpp :: long to string cpp 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =