KNOWING C++ CLASSES
The below program either violates or misses 5 areas of object-oriented programming in C++ including:
Data encapsulation.
Accessibility.
Separate class declaration and definition.
The class is missing some important component even though they're
not significant. In general we should explicitly specify class
components instead of relying on compiler provided
components.
getters must not modify member data.
Syntax error in main function.
Identify the problems (6 of them, 3 pts each), explain the problems in short sentences and provide a code fix.
#include <iostream.h>
class FinalExam {
private:
FinalExam ( int m_score) : score (m_score) { }
public:
int score;
int get_score ( ) { return score ; }
void TakeExam ( ) {
cout << "Start taking exam ....\n";
}
} ;
int main ( ) {
FinalExam final_exam;
final_exam.TakeExam ( );
return 0;
}
Thanks for the question, here are the answers to all the parts.
=======================================================
Problem 1: constructor() should be a public but its declared
as private
Problem 2: int score; is a member variable and should be marked as
private but here its marked as public
Problem 3: when creating an object FinalExam final_exam we
are not passing any intialization value, so it will not
compile
Problem 4: the method are all inline functions that is they are
implemented inside the class body, they should be defined outside
the class
Problem 5: The class should also have a default constructor which
is missing in the class
Problem 6: The class is also missing a copy constructor instead of
relying on the compiler to provide a default one
Problem 7: The class should have a setter function for the member
variable score
======================================================
Here is the corrected code.
#include<iostream>
using namespace std;
class FinalExam{
private:
int score;
public:
FinalExam();
FinalExam(int);
FinalExam(const FinalExam&);
int get_score() const;
void set_score(int);
void TakeExam();
};
FinalExam::FinalExam():score(0){}
FinalExam::FinalExam(int m_score):score(m_score){ }
FinalExam::FinalExam(const FinalExam& exam):
score(exam.score){ }
int FinalExam::get_score() const{return score;}
void FinalExam::set_score(int aScore){
score=aScore;
}void FinalExam::TakeExam(){
cout<<"Take Exam..\n";
}
int main(){
FinalExam final_exam;
final_exam.TakeExam();
}
======================================================
KNOWING C++ CLASSES The below program either violates or misses 5 areas of object-oriented programming in...
C++ OBJECT ORIENTED PROGRAMMING
12. What will be the output of the following C++ code? #include <iostream> #include <vector> using namespace std; int main() { vector<int> myvector; myvector.push_back(78); myvector.push_back(16); myvector.front() += myvector back(); cout << myvector.front() << '\n'; return 0; } a) 78 b) 16 c) 94 d) 86 13. What is the syntax of class template? a) template <paramaters> class declaration b) Template <paramaters> class declaration c) temp <paramaters> class declaration d) Temp <paramaters> class declaration sums the values...
C++ Object Oriented assignment Can you please check the program written below if it has appropriately fulfilled the instructions provided below. Please do the necessary change that this program may need. I am expecting to get a full credit for this assignment so put your effort to correct and help the program have the most efficient algorithm within the scope of the instruction given. INSTRUCTIONS Create a fraction class and add your Name to the name fraction and use this...
I need to do object oriented programming for c++. I had to make make a program where it would add,subtract,multiply,and divide fraction. I got that working, but it wont work for negative can anyone fix it and explain how they did it? Source.cpp code: #include "Fraction.h" #include <iostream> using namespace std; int main() { Fraction f1(-4, 6); Fraction f2(5, -9); Fraction sum = sum.add(f1, f2); sum.print(sum); Fraction diff = diff.subtract(f1, f2); diff.print(diff); ...
Im having trouble with this
C++ program. Lab 10/object/test files provided
LAB 10
1 //Savings.cpp - displays the account balance
at
2 //the end of 1 through 3 years
3 //Created/revised by <your name> on <current
date>
4
5 #include <iostream>
6 #include <iomanip>
7 #include <cmath>
8 using namespace std;
9
10 //function prototype
11 double getBalance(int amount, double rate, int
y);
12
13 int main()
14 {
15 int deposit = 0;
16 double interestRate = 0.0;
17...
Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest to make main.cpp compile. Put the declaration and definition of find youngest in StudentClub.h and StudentClub.cpp separately. You may not modify provided files and only submit StudentClub.h and StudentClub.cpp. Non-member function find youngest is declared as follows. It returns the names of students who have the youngest age. Note there may exist more than one students who are youngest. std::vector find_youngest(const std::vector member); StudentClub...
Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...