C++ Requirements
for Advanced Programming Techniques in Computer Vision
If you can solve the following problems and answer the questions you probably have the basic knowledge of C++.Do not panic if not all of your answers are perfect, but most of them should be. If you would like to review or "activate" you C++ knowledge, you can find my suggestion for basic books here.
I strongly suggest to try out yourself and write these programs before you check the solutions!
Problem #1
Write a small C++ program that prints "Hello World" on the standard output.Solution (select it to appear):
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
Problem #2
Create a header file Person.h Write a small class called Person with two properties: name, age.The class should have a minimal interface that works with the following main program:
#include <iostream>Note: in realty you should separate Person's implmentation to Person.cpp, however, for simplicity the solution defines all function to be inline.
#include "Person.h"
using namespace std;
int main(){
Person p1("Paul", 20);
const Person p2("Peter", 22);
Person p3("James", 24);
cout << p1 << p2 << p3 << endl;
p1 = p2;
p1.setName("John");
cout << p1.getName() << " and " << p2.getName()
<< " are both " << p1.getAge() << " years old.\n";
return 0;
}
Solution (select it to appear):
#ifndef PERSON_H
#define PERSON_H
#include<ostream>
#include<string>
// should never use "using namespace ..." in a global namespace in a header file!
class Person {
public:
Person(std::string s, int a): name(s), age(a) {}
// make get... methods const, so
// - it can be called on const objects
// - to indicate that get... methods do not change the object state
std::string getName() const { return name; }
void setName(std::string s) { name = s; }
int getAge() const { return age; }
void setAge(int a) { age = a; }
private: // in general we do not give access member variables
std::string name;
int age;
};
// Note: the following operator does not have to be friend of Person, because
// it can be efficiently implemented by the public interface of Person.
inline std::ostream& operator<<(std::ostream& out, const Person& p){
out << "Name: " << p.getName()
<< "\nAge: " << p.getAge() << "\n";
return out;
}
#endif // PERSON_H
// For an additional challenge, you can try to use C-like strings (char*)
// instead of std::string. Notice that the implementation will be more
// complicated, since Person has a pointer member, and therefore each object
// has to take care of its memory allocation.
Questions
- What is inheritence?
- What are virtual methods?
- What
is a constructor?
- What is an abstract class?
- When does a programmer need a virtual destructor?
- What is a recursive function?
- What is the difference between the * and the &?