Like a few people already said, dinosaurs came first.
The earliest known feathered dinosaurs were too heavy to fly. Over time, some began to develop a more lightweight bone structure (which birds still have) and formed more distinct wings. These became birds.
Pterodactyls come from a different lineage, not connected to birds. They have the same hollow bone structure but have more in common with with bats (featherless, wings made of stretched skin, evidence of small hairs). That's not to say that bats evolved from them, just that pterodactyls are more closely related to the dinosaurs that became mammals. They don't really fit neatly in any category, and strictly speaking they aren't technically dinosaurs.
Flight does not have a common evolutionary source. Insects developed it first, followed birds and flying mammals who developed their own forms of flight independently. Both birds and mammals branched off from reptiles.